在ManyToMany关系中更新对象休眠

弱者

我的应用程序有2个通过ManyToMany关系链接的java pojo类

@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 班级

@Entity
@Table(name = "cricket_match")
public class Season implements Serializable{

@ManyToMany(mappedBy = "followingSeason", fetch = FetchType.EAGER)
    private Set<User> user;

}

在用户界面上,我有一个季节按钮,单击它可以与用户一起设置季节。db表中有一个预先存在的用户条目以及季节条目。基本上,我正在使用其他季节对象信息来更新用户对象。

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 (?, ?)
德拉甘·博扎诺维奇(Dragan Bozanovic)

是的,您必须从数据库加载关联的所有者。您可以看一下这个答案,它提供了有关如何优化多对多关联的一些机制。

但是,我看到的User实际上是协会的所有者,而不是Season似乎您的映射不正确:mappedBy = "followingMatch"可能不是您的意思mappedBy = "followingSeason"

第三点,您应该对集合(使用复数形式followingSeasonsusers以使代码更具可读性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

休眠中的对象更新

来自分类Dev

如何在休眠中以多对多关系更新表中另一列中的对象数?

来自分类Dev

在Django的ManyToMany关系中按ID添加对象

来自分类Dev

通过多个条件过滤ManyToMany关系中的对象

来自分类Dev

在ManyToMany模型中的Django关系,对象没有属性

来自分类Dev

ManyToMany关系-数据透视表中如何更新属性

来自分类Dev

休眠-从@ManyToMany中删除查询

来自分类Dev

休眠@OneToOne与多个对象的关系?

来自分类Dev

在ManyToMany关系中查询

来自分类Dev

在ManyToMany关系中的Save()

来自分类Dev

休眠部分更新对象

来自分类Dev

更新对象Spring休眠

来自分类Dev

在@ManyToMany休眠关系的运行时中更改获取类型

来自分类Dev

休眠中的特定类型的关系

来自分类Dev

在Django中查询ManyToMany关系

来自分类Dev

在Django中向ManyToMany关系中添加/测试对象的添加问题

来自分类Dev

更新Laravel ManyToMany关系而不删除(或分离)

来自分类Dev

JPA manyToMany关系中间表未更新

来自分类Dev

如何在休眠中删除子对象之前断开父子之间的关系?

来自分类Dev

是否可以使用@manytomany关系仅关联数据库中已经存在的对象?

来自分类Dev

休眠实体中的复杂关系

来自分类Dev

一对多关系中的休眠准则

来自分类Dev

怀疑休眠JPA中的多对多关系

来自分类Dev

休眠,无法从关系中获取数据

来自分类Dev

一对多关系中的休眠准则

来自分类Dev

休眠,无法从关系中获取数据

来自分类Dev

返回休眠中的对象列表

来自分类Dev

在Django中唯一标识ManyToMany关系

来自分类Dev

如何删除JPA @ManyToMany关系中的记录?