我有实体 Profile、Like 和 Place
地方有喜欢。喜欢有参考地点和个人资料。
地点在喜欢上具有 1-N 关系
@PersistenceCapable
public class Place {
@Persistent(mappedBy = "place")
@Element(dependent = "true")
private transient List<Like> likes;
Like 有参考 Profile 和参考 Place
@PersistenceCapable
public class Like implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private Profile profile;
@Persistent
private Place place;
并且配置文件类与该对象没有关系
@PersistenceCapable
public class Profile {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private transient Key key;
使用现有配置文件添加“喜欢”以放置现有地点的最佳方式是什么?
我使用以下代码来做到这一点:
Profile profile;
Place place;
List<Like> likes;
pm = PMF.get().getPersistenceManager();
try {
place = pm.getObjectById(Place.class, placeId);
likes = place.getLikes();
profile = pm.getObjectById(Profile.class, KeyFactory.createKey(Profile.class.getSimpleName(), login));
} finally {
pm.close();
}
likes.add(new Like(place, profile));
place.setLikes(likes);
pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(place);
} finally {
pm.close();
}
并且有 Profile 实体的副本。有办法解决吗?