Open GoogleCodeExporter opened 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
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
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
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
Original issue reported on code.google.com by
chanh.d...@socialdynamx.com
on 28 Oct 2011 at 8:35