Question

I have two classes

Movie
  Collection<Session> sessions;
Session
  Movie movie;

if i define the following method on movie:

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

and then the method on Session:

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

I can save the Movie & Session objects, however I get a stacktrace (below) when i try to retrieve the movie with Movie.findById

however if i dont set the movieId field on the session, then i can retrieve the owning Movie successfully.

Any ideas what i might be doing wrong?

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)
Was it helpful?

Solution

It looks strange that you want to persist Movie and movieId in your Session.

Depending on your use case you can probably make movieId transient or declare it as org.bson.types.ObjectId.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top