我在Spring MVC应用程序中使用了休眠,并且对级联有疑问。我看到许多与此类似的问题,但没有一个可以回答我的问题。假设我有User
和UserPosition
对象。 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
属性应该在哪里?感谢您的时间。 最佳答案
User
是父实体,级联a lways propagates from Parent to Child entities。
因此,用户关联变为:
@OneToMany(mappedBy = "user",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true)
private Collection<UserPosition> userPositionCollection;
但对于默认位置,用户将成为关联的
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);