Hibernate JPA IdentifierGenerationException:为带有@embeddedid的类生成的null id

Jap*_*eva 5 java hibernate jpa hibernate-annotations

我无法将我的数据库域模型映射到程序实体,在一种情况下,实体本质上是一个连接表(一个句点),它结合了另外两个实体(时间段和一天).然后,另一个实体(课程)引用该期间实体,确定它何时发生.

当我尝试使用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
}
Run Code Online (Sandbox Code Playgroud)

嵌入式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
}
Run Code Online (Sandbox Code Playgroud)

然后是使用定义为的时段的课程类:

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
}
Run Code Online (Sandbox Code Playgroud)

Timeslot和Day实体类都是非常基本的pojos,它们的id使用GenerationType.AUTO.所以我的问题是:

  1. 导致此IdentifierGenerationException的原因
  2. 如何在保持相同数据库模型的同时避免它

提前致谢

Dal*_*ton 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;
Run Code Online (Sandbox Code Playgroud)

在PeriodId类中,抛弃那些整数.我已经用这种方式完成了与你类似的映射,但它确实有效.