Hibernate - 如何在关系中正确使用级联

时间:2015-05-12 07:33:38

标签: java hibernate jpa hibernate-mapping cascade

我在我的spring mvc应用程序中使用hibernate并且有关于级联的问题。我看到很多类似的问题,但没有一个能回答我的问题。假设我有UserUserPosition个对象。 User的集合为UserPosition,并且还有一个UserPosition作为默认排名。结构如下所示:

用户:

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Collection<UserPosition> userPositionCollection;

public Collection<UserPosition> getUserPositionCollection() {
    return userPositionCollection;
}

public void setUserPositionCollection(Collection<UserPosition> collection) {
    this.userPositionCollection = collection;
}


 @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "default_User_Position_ID", referencedColumnName = "id")
private UserPosition defaultUserPosition;

public UserPosition getDefaultUserPosition() {
    return defaultUserPosition;
}

public void setDefaultUserPosition(UserPosition defaultUserPosition) {
    this.defaultUserPosition = defaultUserPosition;
}

UserPosition:

@JoinColumn(name = "user_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private User user;

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

@OneToOne(fetch = FetchType.LAZY, mappedBy = "defaultUserPosition", cascade = CascadeType.PERSIST)
private User defaultUserPosition;

public User getDefaultUserPosition() {
    return defaultUserPosition;
}

public void setDefaultUserPosition(User defaultUserPosition) {
    this.defaultUserPosition = defaultUserPosition;
}

现在,我的问题是使用级联保存相关对象的最佳做法是什么?。事实上,我对这三种解决方案感到困惑:

解决方案1:

User user = new User();
//some setters and getters 
UserPosition userPosition = new UserPosition();
//some setters and getters 
List<UserPosition> positionList = new ArrayList<>();
positionList.add(userPosition);
user.setDefaultUserPosition(userPosition);
user.setUserPositionCollection((Collection<UserPosition>) positionList );
session.persist(user)

解决方案2:

User user = new User();
//some setters and getters 
UserPosition userPosition = new UserPosition();
//some setters and getters 
userPosition.setUser(user);
userPosition.setDefaultUserPosition(user);
session.persist(userPosition)

解决方案3(前两个解决方案的组合):

User user = new User();
//some setters and getters 
UserPosition userPosition = new UserPosition();
//some setters and getters 
List<UserPosition> positionList = new ArrayList<>();
positionList.add(userPosition);
user.setDefaultUserPosition(userPosition);
user.setUserPositionCollection((Collection<UserPosition>) positionList );
userPosition.setUser(user);
userPosition.setDefaultUserPosition(user);
session.persist(user);

这对我来说非常重要,所以请帮助我。哪个解决方案是正确的,哪里应该是cascade属性?谢谢你的时间。

1 个答案:

答案 0 :(得分:2)

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) private Collection<UserPosition> userPositionCollection; 是父实体,并级联lways propagates from Parent to Child entities

因此,用户关联成为:

Child

但是对于默认位置,用户成为关联的@OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "default_User_Position_ID", referencedColumnName = "id") private UserPosition defaultUserPosition;

UserPosition

@JoinColumn(name = "user_id", referencedColumnName = "id") @ManyToOne(fetch = FetchType.LAZY) private User user; 课程中是另一种方式:

@OneToOne(fetch = FetchType.LAZY, 
    mappedBy = "defaultUserPosition", 
    cascade = CascadeType.PERSIST)
private User defaultUserPosition;

User

然后,您还必须添加以下始终同步双方的实用程序方法。这些进入public void addUserPosition(UserPosition userPosition) { userPositionCollection.add(userPosition); userPosition.setUser(this); } public void addDefaultUserPosition(UserPosition userPosition) { defaultUserPosition = userPosition; userPosition.setDefaultUserPosition(this); } 类:

User user = new User();
//some setters and getters 
UserPosition userPosition = new UserPosition();
//some setters and getters 

user.addUserPosition(userPosition);
user.setDefaultUserPosition(userPosition);

session.persist(user);

持久性逻辑变为:

mywebsite.com/blogs/{blog title}-{blog id}