Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-2490

@DBRef fetched without ClientSession when using Spring Transactions

    XMLWordPrintable

    Details

      Description

      I made a simple project with two entities, one called Wish and second called Product. I can share the project if needed but it is very simple (I'm using Lombok).
      I'm sharing the details of the entities and the simple repo:

      Repo

      @Repository
      public interface MongoWishRepository extends MongoRepository<Wish, String> {
      }

       Wish

      @Document
      @Data
      @EqualsAndHashCode(onlyExplicitlyIncluded = true)
      @Builder
      public class Wish {
          @Id
          @EqualsAndHashCode.Include
          private String uuid;
          @DBRef
          @CascadeSave
          private Product product;
      
          ....
      }
      

      Product

      @Document
      @Data
      @EqualsAndHashCode(onlyExplicitlyIncluded = true)
      @Builder
      public class Product {
          @Id
          @EqualsAndHashCode.Include
          private String uuid;
          private String brandUuid;
          private String name;
          ....
      }

      In a Test when I execute without @Transactional, it works fine but putting @Transactional at Class level:

      @Test
      public void addWishWithProduct() {
          String productName = "Product";
          Wish wish = Wish.builder()
                  .name("Wish")
                  .product(Product.builder().name(productName).build())
                  .build();
          mongoWishRepository.insert(wish);
          Wish stored = mongoWishRepository.findById(wish.getUuid()).get();
          mongoWishRepository.findAll().stream().forEach(System.out::println);
          Assert.assertTrue(wish.equals(stored));
          Assert.assertTrue(stored.getProduct().getName().equals(productName));
      }
      

      The last line give me NullPointerException as the nested Product of the Wish is not retrieved correctly in the Transaction but I can see that the save of the product works correctly and gave me a UUID.

      Is it related to the Mongo Driver used? What do you suggest to make it working?

        Attachments

          Activity

            People

            Assignee:
            cstrobl Christoph Strobl
            Reporter:
            lionh LionH
            Last updater:
            Mark Paluch
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: