开发者

Hibernate, Oracle, Sequence and One To Many Problem

开发者 https://www.devze.com 2023-02-13 14:37 出处:网络
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 ne

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消