google-code-export / morphia

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

lazy loading with dynamic named reference #336

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What version are you using? (Morphia/Driver/MongoDB) 0.99

Please include a stack trace below:

Exception in thread "main" java.lang.RuntimeException: collection names don't 
match for key and class: Triangle != Test Triangle
    at com.google.code.morphia.DatastoreImpl.getByKey(DatastoreImpl.java:537)
    at com.google.code.morphia.mapping.lazy.proxy.SerializableCollectionObjectReference.fetch(SerializableCollectionObjectReference.java:47)
    at com.google.code.morphia.mapping.lazy.proxy.AbstractReference.get(AbstractReference.java:42)
    at com.thoughtworks.proxy.toys.delegate.DelegatingInvoker.delegate(DelegatingInvoker.java:127)
    at com.thoughtworks.proxy.toys.hotswap.HotSwappingInvoker.delegate(HotSwappingInvoker.java:115)
    at com.thoughtworks.proxy.toys.hotswap.HotSwappingInvoker.invoke(HotSwappingInvoker.java:87)
    at com.google.code.morphia.mapping.lazy.NonFinalizingHotSwappingInvoker.invoke(NonFinalizingHotSwappingInvoker.java:25)
    at com.thoughtworks.proxy.factory.AbstractProxyFactory$CoincidentalInvocationHandlerAdapter.invoke(AbstractProxyFactory.java:96)
    at $java.util.ArrayList$$EnhancerByCGLIB$$39bf8312.get(<generated>)
    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 com.thoughtworks.proxy.toys.delegate.DelegatingInvoker.invokeOnDelegate(DelegatingInvoker.java:164)
    at com.thoughtworks.proxy.toys.delegate.DelegatingInvoker.invoke(DelegatingInvoker.java:116)
    at com.thoughtworks.proxy.toys.dispatch.DispatchingInvoker.invoke(DispatchingInvoker.java:127)
    at com.thoughtworks.proxy.factory.AbstractProxyFactory$CoincidentalInvocationHandlerAdapter.invoke(AbstractProxyFactory.java:96)
    at $java.util.ArrayList$$EnhancerByCGLIB$$c087e85b.get(<generated>)
    at Rectangle.getF(Rectangle.java:60)
    at morp.main(morp.java:231)

Hi, im new to morphia.  And i have an issue with lazy load and dynamic 
collection name.
I have some classes for testing.

Class Rectangle extends Shapes
{
    @Reference
    private Triangle friend;
    ...
}
Class Triangle extends Shapes
{
    ...
}

This works fine if I dont save Triangle in a dynamic collection name. (eg. 
"Test Triangle" instead of let Morphia save them in Triangle).  Changing the 
collection name wont work because the Rectangle will contain something like 
this $ref : Triangle , $id : 1234567890.  So before inserting. i come in and 
change this to $ref : Test Triangle , $id : 1234567890. 

This works correctly.  but I want to have the reference to be lazy (eg. 
@Reference(lazy=true)).  after added the lazy=true to @Reference, I start 
getting the above error.

This is where Im stucked.  Why is it able to match the class without lazy 
loading.  But when lazy loading enabled, it cant match anymore.  Is there a way 
to do this scenario? thanks for your help.

Original issue reported on code.google.com by chanh.d...@socialdynamx.com on 28 Oct 2011 at 8:35

GoogleCodeExporter commented 9 years ago
Can you provide a sample of all the code you are using?

Original comment by scotthernandez on 28 Oct 2011 at 10:58

GoogleCodeExporter commented 9 years ago
Hi Scott,

here is the sample structure of my code.  Thank you for taking time helping me.

//Shape.java
import ...

@Entity
public abstract class Shape
{
    @Id
    protected String id;
    public void setId(String i)
    {
        id = i;
    }
    public String getId()
    {
        return id;
    }

    protected String name;
    public void setName(String n)
    {
        name = n;
    }

    public String getName()
    {
        return name;
    }

}
//end Shape.java

//Rectangle.java
import ...

@Entity
public class Rectangle extends Shape
{
    private Long length;
    public void setLength(Long l)
    {
        length = l;
    }
    public Long getLength()
    {
        return length;
    }

    private Long width;
    public void setWidth(Long w)
    {
        width = w;
    }
    public Long getWidth()
    {
        return width;
    }

    @Reference(lazy = true)
    private List<Triangle> friendly = new ArrayList<Triangle>();
    public void setFriend(Triangle f)
    {
        friendly.add(f);
    }
    public List<Triangle> getFriend()
    {
        return friendly;
    }

    public Triangle getF(int i)
    {
        return friendly.get(i);
    }

    @Reference(lazy = true)
    private Triangle bestFriend;
    public void setBestFriend(Triangle b)
    {
        bestFriend = b;
    }

    public Triangle getBestFriend()
    {
        return bestFriend;
    }    
}
//end Rectangle.java

//Triangle.java
import ...

@Entity(noClassnameStored = true)
@Embedded
public class Triangle extends Shape
{
    public Triangle()
    {
        id = new ObjectId().toString();
    }
    public Triangle(String i)
    {
        id = i;
    }

    @Embedded
    private List<Point> corners = new ArrayList<Point>();   //Point is just a class with x, y value
    public void setCorners(Point a, Point b, Point c)
    {
        corners.add(a);
        corners.add(b);
        corners.add(c);
    }
}
//end Triangle.java

//MorpApp.java

import ...

public class MorpApp
{
    public static void main(String [] args)
    {
        Mongo mongo;

        try
        {
            mongo = new Mongo();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();  
            return;
        }

        //create morphia instance and get the datastore
        Morphia morphia = new Morphia();
        DatastoreImpl ds = (DatastoreImpl)morphia.createDatastore(mongo, "Shapes");

        //create some test objects
        Triangle tri = new Triangle();
        tri.setCorners(new Point(1,2), new Point(3,4), new Point(5,6)  );
        tri.setName("triangle1");

        Triangle tri2 = new Triangle("1");
        tri2.setCorners(new Point(1,2), new Point(3,4), new Point(5,6)  );
        tri2.setName("triangle2");

        Triangle tri3 = new Triangle();
        tri3.setCorners(new Point(1,2), new Point(3,4), new Point(5,6)  );
        tri3.setName("triangle3");

        Rectangle rect = new Rectangle();
        rect.setLength(5l);
        rect.setWidth(2l);
        rect.setName("rectangle");

        //set up the friendly and bestFriend fields
        rect.setFriend(tri);
        rect.setFriend(tri2);
        rect.setBestFriend(tri3);

        //convert the rectangle to a DBObject
        DBObject Obj = morphia.toDBObject(rect);

        //change the DBRef to point to Test Triangle instead of Triangle
        List<DBRef> tris = (List<DBRef>)Obj.get("friendly");
        tris.clear();
        tris.add(new DBRef(null, "Test Triangle", tri.getId()));
        tris.add(new DBRef(null, "Test Triangle", tri2.getId()));

        DBRef bestTri = (DBRef)Obj.get("bestFriend");
        bestTri = new DBRef(null, "Test Triangle", tri3.getId());

        //save the rectangle to 'Test Rectangle' collection
        mongo.getDB("Shapes").getCollection("Test Rectangle").insert(Obj);

        //save the triangles to 'Test Triangle'
        ds.insert("Test Triangle", tri);
        ds.insert("Test Triangle", tri2);
        ds.insert("Test Triangle", tri3);

        //everything is good up until this point, and things saved correctly

        //the problem come when i try to get from the database
        Rectangle getRect = ds.find("Test Rectangle", Rectangle.class).get();

        //when i have (lazy=true) after Reference, this will throw an exception
        //Exception in thread "main" java.lang.RuntimeException: collection names don't match for key and class:      //Triangle != Test Triangle
        //when i dont have (lazy=true) everything works fine, correct data returned
        Triangle getTri = getRect.getF(0);
    }
}

//end MorpApp.java

Original comment by chanh.d...@socialdynamx.com on 31 Oct 2011 at 4:37

GoogleCodeExporter commented 9 years ago
Hi Scott,
I know you are busy, but can you take a look at this issue again.  Thanks.

Original comment by chanh.d...@socialdynamx.com on 7 Nov 2011 at 7:05

GoogleCodeExporter commented 9 years ago
Hi Scott,

If you have time can you look at this issue again? thanks

Original comment by chanh.d...@socialdynamx.com on 6 Dec 2011 at 6:49