I am having an issue with doing a one line save on a one to many object. That foreign key does not get populated in the child objects. Aren't they suppose to automatically from Hibernate? The BadgeID never gets inserted into the 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;
}
@OneToMa开发者_Go百科ny(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;
}
Before calling save on Badge, try the following
......
for(BadgeLevel badgeLevel : badge.getBadegeLevels()
{
badgeLevel.setBadge(badge);
}
repo.save(badge);
精彩评论