I need to have two objects of the same type. By default appengine doesn't allow it, but I found this parameter: datanucleus.appengine.allowMultipleRelationsOfSameType, so I can save the two same type objects.
In debug mode, before calling the makePersistent method I checked a value inside each object and they were differents, however, when I tried to recover the values from the datastore, they were the same. Both had the value of the second object?
This code is to save the object FaseGAE:
manager = GAEDAOFactory.get().getPersistenceManager();
Key faseKey = KeyFactory.stringToKey(grupo.getFaseKey());
FaseGAE faseGAE = manager.getObjectById(FaseGAE.class, faseKey);
faseG开发者_运维百科AE.addGrupoGAE(grupoGAE);
faseGAE = manager.makePersistent(faseGAE);
manager.close();
This code is to get the object:
manager = GAEDAOFactory.get().getPersistenceManager();
FaseGAE faseGAE2 = manager.getObjectById(FaseGAE.class, faseKey);
FaseGAE object:
@PersistenceCapable
public class FaseGAE {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent private List<GrupoGAE> grupos;
GrupoGAE object:
@PersistenceCapable
public class GrupoGAE {
@PrimaryKey
@Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent private List<MyClass1> list;
MyClass1 object:
@PersistenceCapable
public class MyClass1 {
@PrimaryKey
@Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent private MyClass2 sameTypeObject1;
@Persistent private MyClass2 sameTypeObject2;
@Persistent private String testValue1;
@Persistent private String testValue2;
MyClass2 Object:
@PersistenceCapable
public class MyClass2{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
testValue1 and testValue2 keeps different values, but sameTypeObject1 and sameTypeObject2 have the value of sameTypeObject2. I checked the datastore and both objects were created with different values. It seems like both point to the same reference.
Am I doing something wrong?
Something it's missing to work with same type relations? Definitely AppEngine doesn't allow same type relations?I've encountered a similar problem before, I"m not too sure what's your exact problem and whether it's the same. But hope this answer will at least point you in the right direction
However, there are a couple of "best practices" you can adopt when using java with GAE.
1) implement Serializable for classes i.e. public class FaseGAE implements Serializable - this will enable persistent capable classes to be stored and retrieve with session objects.
2) you could try using objectify for GAE datastore http://code.google.com/p/objectify-appengine/
精彩评论