2010-01-13 2 views
2

삭제합니다.JPA 부모의 자녀는 내가 두 기관이

@Transactional 
public void deleteEntity(Object entity) { 
    getEntityManager().remove(entity); 
    getEntityManager().flush(); 
} 

그런 다음 gameObject를로드하려고하면 컬렉션에서 게임이 삭제됩니다. 이 게임은 id 속성이 정확하고 rest는 null입니다. 뭐가 잘못 됐어, 왜이 게임이 아직도 뭉친거야?

답변

2

게임GameObject 엔티티 사이에 양방향 관계가 있기 때문입니다. 게임을 제거하면 관계의 한 쪽만 분리됩니다. 데이터베이스에서 Game -> GameObject 참조를 제거하지만 GameObject -> Game 참조가 유지됩니다. deleteEntity 메소드는 다음과 같이 처리해야합니다.

public void deleteEntity(Game game) { 
    // WARNING - Untested code, only use as an example 
    GameObject gameObject = game.getGameObject(); 
    gameObject.removeFromCollection(game); 
    getEntityManager().remove(game); 
    getEntityManager().merge(gameObject); 
} 

기본 데이터베이스는 외래 키 제약 조건을 지원할 수 있습니다. 즉, JPA 구현이 데이터베이스에서 게임 레코드를 제거하려고 시도 할 때 GameObject의 테이블이 Game 테이블 에있는 외래 키 제약 조건은이 레코드 삭제를 허용하지 않지만 이 null을 허용합니다. 모든 기본 값 (물론 기본 키/ID 제외)의 모든 값