繁体   English   中英

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

[英]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 所以我的问题是:

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

提前致谢

把那些家伙

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM