如何将存储在 MongoDB 中的复合 ID 转换为 Morphia Java 实体对象?
How can I convert a Composite ID stored in a MongoDB into a Morphia Java entity object?
当 _id
是复合 id 时我遇到了问题。然后,我无法将其转换为 Morphia Java 实体对象。
我的 MongoDB 中的 JSON 如下所示:
{
"_id" : {
"id1" : "SAMPLE_ID1",
"id2" : "SAMPLE_ID2"
}
}
Java代码:
import org.mongodb.morphia.annotations.Id;
@Entity(value = "MyCollection", noClassnameStored = true)
public class MyObject {
@Id
private MyObjectId thisId;
//....
}
public class MyObjectId{
@Property("id1")
private String id1;
@Property("id2")
private String id2;
//.....
}
我正在使用吗啡 v1.3.2。
当我插入对象时,出现以下异常:
java.lang.RuntimeException: java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:76)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850)
at org.mongodb.morphia.mapping.Mapper.updateKeyAndVersionInfo(Mapper.java:725)
at org.mongodb.morphia.DatastoreImpl.postSaveOperations(DatastoreImpl.java:1422)
at org.mongodb.morphia.DatastoreImpl.postSaveOperations(DatastoreImpl.java:1408)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1292)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:775)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:758)
.......
当我 select 对象时,我得到以下异常:
java.lang.RuntimeException: java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:76)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:282)
at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:193)
at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134)
at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146)
at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117)
at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:147)
......
我使用的设置非常相似。我认为您需要做的就是将@Entity 添加到 MyObjectId,所以
@Entity(noClassnameStored = true)
public class MyObjectId {
@Property("id1")
private String id1;
@Property("id2")
private String id2;
public MyObjectId() {
}
public MyObjectId(String id1, String id2) {
this.id1 = id1;
this.id2 = id2;
}
}
我似乎记得编组需要无参数构造函数。
当 _id
是复合 id 时我遇到了问题。然后,我无法将其转换为 Morphia Java 实体对象。
我的 MongoDB 中的 JSON 如下所示:
{
"_id" : {
"id1" : "SAMPLE_ID1",
"id2" : "SAMPLE_ID2"
}
}
Java代码:
import org.mongodb.morphia.annotations.Id;
@Entity(value = "MyCollection", noClassnameStored = true)
public class MyObject {
@Id
private MyObjectId thisId;
//....
}
public class MyObjectId{
@Property("id1")
private String id1;
@Property("id2")
private String id2;
//.....
}
我正在使用吗啡 v1.3.2。
当我插入对象时,出现以下异常:
java.lang.RuntimeException: java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:76) at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850) at org.mongodb.morphia.mapping.Mapper.updateKeyAndVersionInfo(Mapper.java:725) at org.mongodb.morphia.DatastoreImpl.postSaveOperations(DatastoreImpl.java:1422) at org.mongodb.morphia.DatastoreImpl.postSaveOperations(DatastoreImpl.java:1408) at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1292) at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:775) at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:758) .......
当我 select 对象时,我得到以下异常:
java.lang.RuntimeException: java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:76) at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850) at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:282) at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:193) at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134) at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146) at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117) at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:147) ......
我使用的设置非常相似。我认为您需要做的就是将@Entity 添加到 MyObjectId,所以
@Entity(noClassnameStored = true)
public class MyObjectId {
@Property("id1")
private String id1;
@Property("id2")
private String id2;
public MyObjectId() {
}
public MyObjectId(String id1, String id2) {
this.id1 = id1;
this.id2 = id2;
}
}
我似乎记得编组需要无参数构造函数。