我无法将我的数据库域模型映射到程序实体,在一种情况下,实体本质上是一个连接表(一个句点),它结合了另外两个实体(时间段和一天)。然后,另一个实体(课程)引用该期间实体,确定它何时发生。
当我尝试使用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
。所以我的问题是:
提前致谢
答案 0 :(得分:5)
把那些家伙
@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类中,抛弃那些整数。我已经用这种方式完成了与你类似的映射,但它确实有效。
答案 1 :(得分:1)
我能够为我的案例(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
}
答案 2 :(得分:0)
乍一看, 您在嵌入式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
}