gpt4 book ai didi

java - 如何更新具有 OneToMany 关联的对象的子行

转载 作者:行者123 更新时间:2023-12-02 05:11:10 24 4
gpt4 key购买 nike

我正在开发一个 Java 项目,在尝试使用 oneToMany 关联更新对象时遇到了困难,以下是我的类:

团队.class

    @Entity
@Table(name = "TEAM", schema = SchemaRef.NOM_SCHEMA_REF)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheRegion.CACHE_TEAM)
public class TEAM extends TeamPrincipal {

/**
* long serialVersionUID.
*/
private static final long serialVersionUID = -7697301560938121726L;

/** The players . */
@OneToMany(mappedBy = "tm", cascade = CascadeType.ALL, orphanRemoval = false)
@Fetch(FetchMode.SUBSELECT)
@LazyCollection(LazyCollectionOption.FALSE)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheRegion.CACHE_TEAM)
private Set<Player> listPlayers;

//Getters & Setters

}

玩家等级

@Entity
@Table(name = "PLAYER", schema = SchemaReferentiel.NOM_SCHEMA_REF)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheRegion.CACHE_TEAM)
public class Player extends DomainObject {

/**
* long serialVersionUID.
*/
private static final long serialVersionUID = -7697301560938121726L;

/** The id. */
@Id
@SequenceGenerator(
name = "SequenceIdGenerator",
sequenceName = SchemaReferentiel.NOM_SCHEMA_REF + ".SEQUENCE_PLAYER",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceIdGenerator")
@Column(name = "PLAYER_ID")
private long id;

/** The Player. */
@ManyToOne
@JoinColumn(name = "lTeam_Player", referencedColumnName = "id", nullable = false)
@ForeignKey(name = "C_FK_PlTeam_Player")
private Team team;

//getters and setter
}

调用DAO方法是TeamDao.java:

sessionFactory.getCurrentSession().saveOrUpdate(toSave);

我在此映射中遇到的问题是,当我尝试通过更改玩家列表来更新团队时,它会更新团队属性,但玩家始终是与添加时相同。

创建正确完成,在 Debug模式下尝试更新,团队包含要更新的新玩家列表,调用 DAO 方法后,父级(TEAM)已更新,但 children (玩家)则不然

最佳答案

由于这是双向映射,因此请确保为团队设置玩家并为每个玩家设置团队:

team.setPlayers(players);

for(Player player: players){
player.setTeam(team);
}

sessionFactory.getCurrentSession().saveOrUpdate(team);

关于java - 如何更新具有 OneToMany 关联的对象的子行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56324810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com