두 엔티티 사이에 관계가 있습니다.부모 엔티티에 대한 기존 자식 엔티티 할당 JPA-GAE
두 엔터티가 이미 만들어져 DB에 삽입되었습니다. 사용자는 어느 시점에서 이들 사이를 연결할 수 있습니다. 메신저는 점점 메신저 일을하려고하면 (WannaMeetUser로 식별 WannaMeetUser ("123456")의 부모로 WannaMeetUser ("654321")를 설립 감지 시도하지만 엔티티 :
javax.persistence.PersistenceException을 "123456")은 이미 부모없이 지속 된 입니다. 개체가 보존 된 후에 부모를 설정하거나 변경할 수 없습니다 ( ).
이 (사용자가 사용자의 왕에서 많은 친구를 가질 수 있습니다) 타동사의 관계입니다 :
코드가 부착되어
@Entity
public class WannaMeetUser {
@Id //signifies the primary key
@Column(name = "ID", nullable = false)
private Key id;
@ManyToMany
@Basic
private List<WannaMeetUser> userFriends = new ArrayList<WannaMeetUser>();
}
public void addFriendToWannaMeetUser(@Named("userId") String userId,
@Named("friendId") String friendId) {
EntityManager mgr = getEntityManager();
try
{
WannaMeetUser user = mgr.find(WannaMeetUser.class, WannaMeetServerUtils.getKeyFromString("WannaMeetUser", userId));
WannaMeetUser friend = mgr.find(WannaMeetUser.class, WannaMeetServerUtils.getKeyFromString("WannaMeetUser", friendId));
String coupleId = getcoupleId(userId.toString(), friendId.toString());
if (friend == null || user == null) {
throw new EntityNotFoundException("Object does not exist");
}
WannaMeetCouple couple=mgr.find(WannaMeetCouple.class, coupleId);
if (couple == null) {
couple = createCouple(userId.toString(), friendId.toString());
couple.setId(coupleId);
setUserJoined(couple, userId.toString(), friendId.toString(), true);
}
else {
if (isFriendAllready(couple, userId.toString(), friendId.toString()))
;
setUserJoined(couple, userId.toString(), friendId.toString(), false);
doAddFriend(user, friend, 10, 12321321);
mgr.persist(couple);
mgr.persist(friend);
mgr.persist(user);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
mgr.close();
}
}
내 질문 상자 등을 가장 좋은 방법은 무엇입니까 관계 ?
감사
의견을 주셔서 감사합니다. 그러나 관계가 사용자 작성에 알려지지 않았으므로 제안 사항이 저에게 관련되지 않습니다. –