我正在研究Java项目,在尝试使用oneToMany关联更新对象时遇到问题,这是我的类:
Team.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
}
Player.class
@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);
此映射所面临的问题是,当我尝试通过更改球员列表来更新团队时,它会更新 Team (团队)属性,但 Players (参与者)始终是与添加时相同。
创建正确完成,在尝试更新的调试模式下,团队包含要更新的新玩家列表,在调用DAO方法后,父级( TEAM )已更新,但孩子(玩家)不是
答案 0 :(得分:0)
由于这是双向映射,因此请确保为团队设置了球员,并为每个球员设置了团队:
team.setPlayers(players);
for(Player player: players){
player.setTeam(team);
}
sessionFactory.getCurrentSession().saveOrUpdate(team);