在morphia模型中持久拥有对象id

时间:2011-12-22 05:49:23

标签: playframework morphia mongodb-java

我有两个班级

Movie
  Collection<Session> sessions;
Session
  Movie movie;

如果我在电影上定义以下方法:

addSession() {
    this.sessions.add(session);
    session.setMovie(this);
}

然后是Session上的方法:

Object movieId;
setMovie(Movie movie) {
    this.movieId  = movie.getId();
}

我可以保存电影&amp;会话对象,但当我尝试使用Movie.findById

检索电影时,我得到一个堆栈跟踪(下面)

但是,如果我没有在会话上设置movieId字段,那么我可以成功检索拥有的电影。

任何想法我可能做错了什么?

java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject
    at com.google.code.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:136)
    at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:507)
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:484)
    at com.google.code.morphia.mapping.ReferenceMapper.resolveObject(ReferenceMapper.java:277)
    at com.google.code.morphia.mapping.ReferenceMapper.readCollection(ReferenceMapper.java:225)
    at com.google.code.morphia.mapping.ReferenceMapper.fromDBObject(ReferenceMapper.java:143)
    at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:505)
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:484)
    at com.google.code.morphia.mapping.Mapper.fromDBObject(Mapper.java:267)
    at com.google.code.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:66)
    at com.google.code.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:53)
    at com.google.code.morphia.query.MorphiaIterator.next(MorphiaIterator.java:48)
    at com.google.code.morphia.query.QueryImpl.get(QueryImpl.java:365)
    at play.modules.morphia.Model$MorphiaQuery._get(Model.java:1045)
    at models.morphia.Movie.findById(Movie.java)
    at models.MovieTest.testSave(MovieTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at play.test.PlayJUnitRunner$StartPlay$1$1$1.execute(PlayJUnitRunner.java:73)
    at play.Invoker$Invocation.run(Invoker.java:265)
    at play.Invoker.invokeInThread(Invoker.java:67)
    at play.test.PlayJUnitRunner$StartPlay$1$1.evaluate(PlayJUnitRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:0)

您希望在Movie中保留movieIdSession,这一点很奇怪。

根据您的使用情况,您可以暂时movieId或将其声明为org.bson.types.ObjectId