2012-02-13 95 views
0

我對提出這個問題的必要條件有點不熟悉,但是我們會看看我能否把它做對。允許JPA @ManyToOne關係中的空引用(Play!Framework)

我有一個JPA實體代表加入其他幾個實體,這就是所謂UserJump:

@Entity 
public class UserJump extends Model{ 

    @ManyToOne 
    public User user; 
    @ManyToOne 
    public JumpSession jumpSession; 
    @ManyToOne 
    public Parachute parachute; 
} 

我有一個JumpSession類是指回UserJump:

@Entity 
public class JumpSession extends GenericModel{ 

    @OneToMany(mappedBy="jumpSession") 
    public List<UserJump> userJumps; 
} 

然而,我需要能夠刪除JumpSession對象,同時保留引用它們的任何UserJump對象(當我在JumpSession上調用delete()時,我得到一個ConstraintViolationException),因爲UserJump對象仍將其他獨特信息鏈接在一起。理想情況下,UserJump中的jumpSession變量將更改爲null

我該怎麼做?

回答

0

你只需要刪除JumpSession之前修改UserJump:

for (UserJump uj : jumpSession.getUserJumps()) { 
    uj.setJumpSession(null); 
    // now the UserJump doesn't reference the soon-to-be-deleted JumpSession anymore 
} 
session.delete(jumpSession); 

(注:上面是傳統的Java Hibernate代碼,我不知道怎麼翻譯,這樣做的Play的方式)

相關問題