2012-10-15 45 views
0

我使用Spring Data来管理我的MongoDB数据库。Mongo Spring Data“foreign”key

在一个集合上,我有2个ObjectId字段,_id和另一个引用另一个集合的实例。

我收藏的类是这样的:

public class CollectionA 
{ 
private ObjectId id; 
private String collectionADescription 
private ObjectId collectionBId 
} 

,像这样规定的I类是能够插入了“外键”成功地为$ OID。

的问题是,我得到以下绑定错误:如果我设置为字符串变量插入的字符串而不是$ OID

我怎样才能做到这一点的collectionBId变量

Field error in object 'collectionA' on field 'collectionBId': rejected value []; codes [typeMismatch.collectionA.collectionBId,typeMismatch.collectionBId,typeMismatch.org.bson.types.ObjectId,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [collectionA.collectionBId,collectionBId]; arguments []; default message [collectionBId]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.bson.types.ObjectId' for property 'collectionBId'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.bson.types.ObjectId] for property 'collectionBId': no matching editors or conversion strategy found]

Thans

回答

2

如果你真的想在你的数据存储关系,你应该使用Spring数据MongoDB的@DBRef注解。但我建议您重新考虑/重新设计您的模式,以利用嵌入式文档。也许this文件将帮助你设计更好的模式。