google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Lazy referencing fails with multiple databases (MappingException) #321

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Versions:
morphia latest (r1762)
Java driver 2.6.5
MongoDB 1.9.1

Lazy references don't work after a second Datastore is created (to connect to a 
second db). I suspect this is related to the DatastoreHolder singleton - the 
second Datastore overwrites the first one.

The problem does not occur without lazy referencing. Neither does it occur when 
only one Datastore is created.

Code to reproduce:

@Entity("users")
public class User
{
   @Id public ObjectId id;
}

---

@Entity("posts")
public class Post
{
   @Id public ObjectId id;

   @Reference( lazy = true )
   public User author;
}

---

public class MorphiaTestCase
{
   public static void main( String[] args ) throws UnknownHostException, MongoException
   {
      Morphia morphia = new Morphia();
      morphia.mapPackageFromClass( Post.class );
      Mongo mongo = new Mongo();
      Datastore ds1 = morphia.createDatastore( mongo, "test1" );
      Datastore ds2 = morphia.createDatastore( mongo, "test2" );

      // step 1: create user document in collection "users"
      User user = new User();
      ds1.save(user);

      // step 2: create post document in collection "docs", referencing the user document created above
      Post doc = new Post();
      doc.author = user;
      ds1.save( doc );

      // trying to retrieve the post document created in step 2 causes a MappingException
      ds1.find( Post.class ).asList();
   }
}

---

Stack trace:

Exception in thread "main" java.lang.RuntimeException: 
com.google.code.morphia.mapping.MappingException: The reference({ "$ref" : 
"users", "$id" : "4e6797414b901d7e1b811bfc" }) could not be fetched for 
com.test.Post.author
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:505)
    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.asList(QueryImpl.java:255)
    at com.test.MorphiaTestCase.main(MorphiaTestCase.java:30)
Caused by: com.google.code.morphia.mapping.MappingException: The reference({ 
"$ref" : "users", "$id" : "4e6797414b901d7e1b811bfc" }) could not be fetched 
for com.test.Post.author
    at com.google.code.morphia.mapping.ReferenceMapper.readSingle(ReferenceMapper.java:163)
    at com.google.code.morphia.mapping.ReferenceMapper.fromDBObject(ReferenceMapper.java:145)
    at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:523)
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:502)
    ... 6 more

Original issue reported on code.google.com by sdeit...@googlemail.com on 7 Sep 2011 at 4:15

GoogleCodeExporter commented 9 years ago
Btw, using separate Mongo and/or Morphia objects for the second Datastore 
doesn't help, which seems to argue for the DatastoreHolder singleton (or some 
other static data) being the cause.

Original comment by sdeit...@googlemail.com on 7 Sep 2011 at 4:18

GoogleCodeExporter commented 9 years ago
We came across the same problem. Our situation is that we wanted physically 
separate db's.; so we had setup separate Mongo/Morphia objects.

The DatastoreHolder is the problem. We actually replaced the 
DefaultDatastoreProvider, and added a method to Mapper to fix this issue. I've 
attached our rebuild of morphia-1.0-SNAPSHOT.jar.

Am happy to share the source; but I'm pretty sure that our solution is NOT the 
direction the morphia team was headed. I was just trying to get past this issue.

Original comment by john.fow...@appconomy.com on 18 Oct 2011 at 4:27

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks John for the JAR: I was struggling for hours with this issue. Your fix 
should be integrated in master.

Original comment by ma...@mashape.com on 13 Apr 2012 at 2:42