Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Document M2: Bug? with @DBRef Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Document M2: Bug? with @DBRef

    Hi there,

    i think i have found a bug using @DBRef.
    The following Code is taken from [1] referenced in a forum post [2].

    I have Foobar which has a List of CamelCase Classes. These CamelCase Classes should be stored in a different collection, so I use @DBRef. The ids of both Classes
    are set before saving.

    Heres the Code of the classes.

    Code:
    @Document(collection = "foobar")
    public class Foobar implements Serializable {
    
    	private static final long serialVersionUID = 1L;
    
    	@Id
    	private String id;
    	
    	@DBRef(collection="camelCaseClass")
    	private List<CamelCaseClass> refs = new ArrayList<CamelCaseClass>();
    [...]
    Code:
    @Document(collection= "camelCaseClass")
    public class CamelCaseClass implements Serializable {
    
    	private static final long serialVersionUID = 1L;
    
    	@Id
    	private String id;
    	
    	private String name;
    [...]
    The Test Class. Ids are set before saving.

    Code:
    @ContextConfiguration(locations = "classpath:META-INF/spring/context.xml")
    public class DBRefTestCase extends AbstractJUnit4SpringContextTests {
    
    	@Autowired
    	private FoobarRepository foobarRepository;
    	
    	@Autowired
    	private CamelCaseRepository ccRepository;
    	
    	@Autowired
    	private MongoTemplate template;
    	
    	@Before
    	@After
    	public void cleanUp() {
    		foobarRepository.deleteAll();
    		ccRepository.deleteAll();
    	}
    	
    	@Test
    	public void testDBRef() {
    		
    		CamelCaseClass foo1 = new CamelCaseClass();
    		foo1.setName("foo1");
    		foo1.setId("12345"); //without its working
    
    		CamelCaseClass foo2 = new CamelCaseClass();
    		foo2.setName("foo2");
                    foo2.setId("67890"); //without its working
    		
    		Foobar foobar = new Foobar();
    		
    		foobar.getRefs().add(foo1);
    		foobar.getRefs().add(foo2);		
    		
    		ccRepository.save(foo1);
    		ccRepository.save(foo2);
    		
    		foobar = foobarRepository.save(foobar);
    		
    		assertNotNull(foobar.getId());
    		
    		foobar = foobarRepository.findById(foobar.getId());
    		
    		assertTrue(foobar.getRefs().contains(foo1));
    		assertTrue(foobar.getRefs().contains(foo2));		
    	}
    [...]
    So when i run this Test, I get the following Exception
    Code:
    Exception in thread "main" org.springframework.core.convert.ConversionFailedException: Unable to convert value "12345" from type 'java.lang.String' to type 'org.bson.types.ObjectId'; nested exception is java.lang.IllegalArgumentException: invalid ObjectId [12345]
    	at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:73)
    	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:36)
    	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:184)
    	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:133)
    	at org.springframework.data.mapping.BeanWrapper.getPotentiallyConvertedValue(BeanWrapper.java:224)
    	at org.springframework.data.mapping.BeanWrapper.getProperty(BeanWrapper.java:207)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.createDBRef(MappingMongoConverter.java:583)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writeCollectionInternal(MappingMongoConverter.java:458)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:407)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter$5.doWithAssociation(MappingMongoConverter.java:387)
    	at org.springframework.data.mapping.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:119)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:374)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:316)
    	at org.springframework.data.document.mongodb.convert.MappingMongoConverter.write(MappingMongoConverter.java:286)
    	at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:606)
    	at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:599)
    If i dont set an ID value before saving its working and an ID is generated.

    I would be glad to get any hints or clues.

    Best regards.
    bjoern



    [1] http://ethone.de/spring/mongodb-samples.zip
    [2] http://forum.springsource.org/showth...859#post360859
Working...
X