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

Stackoverflow when querying document with cycle reference using @DBRef

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 1.0.2
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Environment:
      linux mint 13, java 7, mongodb 2.0.4

      Description

      Given two documents which reference each other with a DBRef

      @Document
      class Site {
          @DBRef User admin;
      }
      
      @Document
      class User {
          @DBRef Site site;
      }
      
      Site siteSaved = siteRepository.save(new Site());
      User userSaved = userRepository.save(new User(siteSaved));
      siteSaved.setAdmin(userSaved);
      siteRepository.save(siteSaved);
      
      siteRepository.findOne(siteSaved.id); // stackoverflow here
      
      java.lang.StackOverflowError
      	at org.bson.BasicBSONEncoder._put(BasicBSONEncoder.java:417)
      	at org.bson.BasicBSONEncoder.putObjectId(BasicBSONEncoder.java:388)
      	at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:188)
      	at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:121)
      	at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
      	at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
      	at com.mongodb.OutMessage.putObject(OutMessage.java:142)
      	at com.mongodb.OutMessage._appendQuery(OutMessage.java:85)
      	at com.mongodb.OutMessage.query(OutMessage.java:44)
      	at com.mongodb.OutMessage.query(OutMessage.java:38)
      	at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:303)
      	at com.mongodb.DBCollection.findOne(DBCollection.java:332)
      	at com.mongodb.DBCollection.findOne(DBCollection.java:318)
      	at com.mongodb.DBRefBase.fetch(DBRefBase.java:52)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DelegatingParameterValueProvider.getParameterValue(MappingMongoConverter.java:950)
      	at org.springframework.data.mapping.model.BeanWrapper.<init>(BeanWrapper.java:115)
      	at org.springframework.data.mapping.model.BeanWrapper.create(BeanWrapper.java:73)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:217)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:200)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:712)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$2.doWithAssociation(MappingMongoConverter.java:239)
      	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:185)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:236)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:200)
      	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DelegatingParameterValueProvider.getParameterValue(MappingMongoConverter.java:950)
      	at org.springframework.data.mapping.model.BeanWrapper.<init>(BeanWrapper.java:115)
      	at org.springframework.data.mapping.model.BeanWrapper.create(BeanWrapper.java:73)
      

      As a workaround I can break this cycle and only reference Site from User and add an isAdmin property to User class but I think it is less direct and clear this way. It requires one more query to retrieve the admin of a Site.

      I fell that it is because retrieving a document from DBRef is not lazy as asked in DATAMONGO-348, but I thought it was eager only for one level depth.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olivergierke Oliver Gierke
                Reporter:
                ludovic.praud Ludovic Praud
                Last updater:
                Christoph Strobl
              • Votes:
                8 Vote for this issue
                Watchers:
                9 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: