Hibernate,Oracle,序列和一对多问题

时间:2011-02-23 23:03:27

标签: oracle hibernate save one-to-many

我遇到了在一对多对象上保存一行的问题。该外键不会填充在子对象中。他们不是想从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;
}

1 个答案:

答案 0 :(得分:0)

在调用保存徽章之前,请尝试以下

......

for(BadgeLevel badgeLevel : badge.getBadegeLevels()
{
   badgeLevel.setBadge(badge);
}
repo.save(badge);