![](/img/trans.png)
[英].IdentifierGenerationException: null id generated for:class
[英]Hibernate JPA IdentifierGenerationException: null id generated for class with @embeddedid
我無法將我的數據庫域模型映射到程序實體,在一種情況下,實體本質上是一個連接表(一個句點),它結合了另外兩個實體(時間段和一天)。 然后,另一個實體(課程)引用該期間實體,確定它何時發生。
當我嘗試使用saveOrUpdate(lesson)
以新的時段保存課程時,hibernate會拋出一個IdentifierGenerationException
org.hibernate.id.IdentifierGenerationException:為:class com.trials.domain.Period生成的null id
數據庫如下所示(不是真正的數據庫,只是關鍵表和列)
在java hibernate模型中,我使用了一個嵌入式id作為句點類的主鍵,然后課程類引用了一個句點。
Period.java
@Entity
@Table(name = "period")
public class Period{
@EmbeddedId
private PeriodId periodId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "day_idday", nullable = false, insertable = false, updatable = false)
private Day day;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "timeslot_idtimeslot", nullable = false, insertable = false, updatable = false)
private Timeslot timeslot;
//constructors, getters, setters, hashcode, and equals
}
嵌入式ID只有主鍵列:
PeriodId.java
@Embeddable
public class PeriodId implements Serializable {
@Column(name = "timeslot_idtimeslot")
private int timeslotId;
@Column(name = "day_idday")
private int dayId;
//constructors, getters, setters, hashcode, and equals
}
然后是使用定義為的時段的課程類:
Lesson.java
@Entity
@Table(name = "lesson")
public class Lesson {
@Id
@Column(name = "idlesson")
private int lessonId;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumns({@JoinColumn(name = "period_timeslot_idtimeslot", nullable = false, updatable = false), @JoinColumn(name = "period_day_idday", nullable = false, updatable = false)})
private Period period;
//constructors, getters, setters, hashcode, and equals
}
Timeslot和Day實體類都是非常基本的pojos,它們的id使用GenerationType.AUTO
。 所以我的問題是:
提前致謝
把那些家伙
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "day_idday", nullable = false, insertable = false, updatable = false)
private Day day;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "timeslot_idtimeslot", nullable = false, insertable = false, updatable = false)
private Timeslot timeslot;
在PeriodId類中,拋棄那些整數。 我已經用這種方式完成了與你類似的映射,並且它可以工作。
我能夠為我的案例(scala代碼)創建以下映射,並且可以完全拋棄@Embeddable類:
@Entity
@Table(name = "payment_order_item", schema = "pg")
@IdClass(classOf[PaymentOrderItem])
final class PaymentOrderItem extends Serializable{
@Id
@ManyToOne
@JoinColumn(name = "order_item_id", referencedColumnName = "id")
var orderItem: OrderItem = _
@Id
@ManyToOne
@JoinColumn(name = "payment_id", referencedColumnName = "id")
var payment: Payment = _
}
那么以下內容應該對你有用
@Entity
@Table(name = "period")
@IdClass(Period.class)
public class Period extends Serializable{
@Id
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "day_idday", referencedColumnName = "id", nullable = false)
private Day day;
@Id
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "timeslot_idtimeslot", referencedColumnName = "id", nullable = false)
private Timeslot timeslot;
//constructors, getters, setters, hashcode, and equals
}
乍一看,您在嵌入式id類中缺少生成的值注釋。
@Embeddable
public class PeriodId implements Serializable {
@GeneratedValue
@Column(name = "timeslot_idtimeslot")
private int timeslotId;
@GeneratedValue
@Column(name = "day_idday")
private int dayId;
//constructors, getters, setters, hashcode, and equals
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.