我遇到了在一对多对象上保存一行的问题。该外键不会填充在子对象中。他们不是想从Hibernate自动进行的吗? BadgeID永远不会插入BadgeLevel.BadgeID。
Badge.java
@Basic
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BADGE_SEQUENCE")
@SequenceGenerator(name="BADGE_SEQUENCE", sequenceName = "BADGE_SEQUENCE")
@Column(name = "ID", nullable=false, unique=true)
public Long getId() {
return id;
}
@OneToMany(mappedBy="badge", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@Fetch(value=FetchMode.SELECT)
public List<BadgeLevel> getBadgeLevels() {
return this.badgelevels;
}
BadgeLevel.java
@Basic
@Id
@NotNull
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BADGELEVEL_SEQUENCE")
@SequenceGenerator(name="BADGELEVEL_SEQUENCE", sequenceName = "BADGELEVEL_SEQUENCE")
@Column(name = "ID", nullable=false, unique=true)
public Long getId() {
return id;
}
@ManyToOne()
@JoinColumn(name = "BADGEID")
public Badge getBadge() {
return this.badge;
}
/**
* set badge
*/
public void setBadge(Badge badge) {
this.badge = badge;
}
答案 0 :(得分:0)
在调用保存徽章之前,请尝试以下
......
for(BadgeLevel badgeLevel : badge.getBadegeLevels()
{
badgeLevel.setBadge(badge);
}
repo.save(badge);