更新多对多关系休眠中的对象
Updating objects in ManyToMany relationship hibernate
我的应用程序有 2 个 java pojo class 通过 ManyToMany
关系链接:
@Entity
@Table(name = "user")
public class User implements Serializable {
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "user_match", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "match_id") })
private Set<Season> followingSeason;
}
Season
class
@Entity
@Table(name = "cricket_match")
public class Season implements Serializable{
@ManyToMany(mappedBy = "followingSeason", fetch = FetchType.EAGER)
private Set<User> user;
}
在 UI 我有一个季节按钮,点击它可以设置用户的季节。数据库 table 中有一个预先存在的用户条目以及季节条目。基本上我是用一个额外的季节对象信息更新用户对象。
if ("true".equalsIgnoreCase(followSeason)) {
Season season = new Season(); // I cannot instantiate a new season object here as it clears all the pre-existing entries of the season row of the entered season id. Do I load the season object first & then set it with the user object?
season.setSeasonId(SeasonId);
Set<Season> seasonSet = new HashSet<Season>();
seasonSet.add(season);
loggedInUser.setFollowingSeason(seasonSet); //loggedInUser is a User object
this.myService.followSeason(loggedInUser);
我无法在此处实例化新的季节对象,因为它会清除输入的季节 ID 的季节行中所有预先存在的条目。我是否首先从数据库加载季节对象,然后使用用户对象设置它?我不确定这是否是正确的方法。
public void followSeason(User loggedInUser){
sessionFactory.getCurrentSession().update(loggedInUser);
}
来自日志
Hibernate: delete from user_season where user_id=?
2015-12-30 18:16:03.0999 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Done deleting collection
2015-12-30 18:16:04.0005 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Inserting collection: [.User.followingSeason#1]
2015-12-30 18:16:04.0011 DEBUG http-bio-8080-exec-6 org.hibernate.SQL – insert into user_season (user_id, season_id) values (?, ?)
Hibernate: insert into user_season (user_id, season_id) values (?, ?)
是的,您必须从数据库加载关联的所有者。您可以查看此 ,它提供了一些有关如何优化 many-to-many 关联的机制。
但是,我看到 User
实际上是协会的所有者,而不是 Season
。而且您的映射似乎不正确:您可能是指 mappedBy = "followingSeason"
.
而不是 mappedBy = "followingMatch"
第三点,集合应该使用复数(followingSeasons
,users
),使代码更易读
我的应用程序有 2 个 java pojo class 通过 ManyToMany
关系链接:
@Entity
@Table(name = "user")
public class User implements Serializable {
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "user_match", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "match_id") })
private Set<Season> followingSeason;
}
Season
class
@Entity
@Table(name = "cricket_match")
public class Season implements Serializable{
@ManyToMany(mappedBy = "followingSeason", fetch = FetchType.EAGER)
private Set<User> user;
}
在 UI 我有一个季节按钮,点击它可以设置用户的季节。数据库 table 中有一个预先存在的用户条目以及季节条目。基本上我是用一个额外的季节对象信息更新用户对象。
if ("true".equalsIgnoreCase(followSeason)) {
Season season = new Season(); // I cannot instantiate a new season object here as it clears all the pre-existing entries of the season row of the entered season id. Do I load the season object first & then set it with the user object?
season.setSeasonId(SeasonId);
Set<Season> seasonSet = new HashSet<Season>();
seasonSet.add(season);
loggedInUser.setFollowingSeason(seasonSet); //loggedInUser is a User object
this.myService.followSeason(loggedInUser);
我无法在此处实例化新的季节对象,因为它会清除输入的季节 ID 的季节行中所有预先存在的条目。我是否首先从数据库加载季节对象,然后使用用户对象设置它?我不确定这是否是正确的方法。
public void followSeason(User loggedInUser){
sessionFactory.getCurrentSession().update(loggedInUser);
}
来自日志
Hibernate: delete from user_season where user_id=?
2015-12-30 18:16:03.0999 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Done deleting collection
2015-12-30 18:16:04.0005 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Inserting collection: [.User.followingSeason#1]
2015-12-30 18:16:04.0011 DEBUG http-bio-8080-exec-6 org.hibernate.SQL – insert into user_season (user_id, season_id) values (?, ?)
Hibernate: insert into user_season (user_id, season_id) values (?, ?)
是的,您必须从数据库加载关联的所有者。您可以查看此
但是,我看到 User
实际上是协会的所有者,而不是 Season
。而且您的映射似乎不正确:您可能是指 mappedBy = "followingSeason"
.
mappedBy = "followingMatch"
第三点,集合应该使用复数(followingSeasons
,users
),使代码更易读