Hibernate JPA IdentifierGenerationException:使用@embeddedid为类生成的空id [英] Hibernate JPA IdentifierGenerationException: null id generated for class with @embeddedid

查看:296
本文介绍了Hibernate JPA IdentifierGenerationException:使用@embeddedid为类生成的空id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一种情况下,我的数据库域模型映射到程序实体时遇到问题,在这种情况下,实体本质上是一个连接两个其他实体(时隙和一天)的连接表(一个时段)。另一个实体(一节课)然后提及这个期间实体,确定它何时发生。



当我尝试使用<$ c保存新课程的课程时$ c> saveOrUpdate(lesson) hibernate抛出一个IdentifierGenerationException异常


$ b


org.hibernate.id.IdentifierGenerationException:null id生成的:class com.trials.domain.Period

数据库如下所示(不是真正的数据库,只是关键表和栏目)





在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)
私人Timeslot时隙;

//构造函数,getters,setters,hashcode,等于
}

嵌入式ID仅包含主键列:



PeriodId.java

  @Embeddable 
public class PeriodId implements Serializable {
@Column(name =timeslot_idtimeslot)
private int timeslotId;

@Column(name =day_idday)
private int dayId;

//构造函数,getters,setters,hashcode,等于
}

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

Lesson.java

  @Entity 
@Table(name =lesson)
public class Lesson {
@Id
@ Column(name =idlesson)
private int lessonId;
$ b @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期间期间;
//构造函数,getters,setters,hashcode,等于
}

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)
私人Timeslot时隙;

并丢弃这些整数。我以这种方式完成了一个类似于你的映射,并且它可以工作。


I am having trouble mapping my database domain model to the program entities in one case where the entity is essentially a join table (a period) which combines two other entities (a timeslot and a day). Another entity (a lesson) then has a reference to this period entity, determining when it occurs.

When I try to save a lesson with a new period using saveOrUpdate(lesson) hibernate throws an IdentifierGenerationException

org.hibernate.id.IdentifierGenerationException: null id generated for:class com.trials.domain.Period

The database looks like below (not the real database, just the key tables and columns)

In the java hibernate model, I have used an embedded id for the primary key of the period class and the lesson class then has a reference to a period.

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
}

And the embedded id just has the primary key columns:

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
}

Then there is the lesson class that uses the period defined as:

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
}

The Timeslot and Day entity classes are both very basic pojos, and their ids use GenerationType.AUTO. So my problems are:

  1. What causes this IdentifierGenerationException
  2. How to avoid it while keeping the same database model

Thanks in advance

解决方案

Put those guys

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

inside the PeriodId class and throw away those ints. I have done a mapping similar to yours this way and it works.

这篇关于Hibernate JPA IdentifierGenerationException:使用@embeddedid为类生成的空id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆