简体   繁体   English

当我尝试创建插入查询时如何使用休眠@EmbeddedId

[英]how to use hibernate @EmbeddedId when i try to create insert query

I am using hibernate and i have created entities on the java side using hibernates. 我正在使用休眠,并且我已经在Java端使用休眠创建了实体。 One of my DB table has multiple columns as primary key and hibernate used @EmbeddedId to deal with that. 我的一个数据库表有多个列作为主键,休眠使用@EmbeddedId来处理。

My problem is that when i create an insert query i get this error: 我的问题是,当我创建插入查询时,出现此错误:

"null id generated for:class com.iu.eblood.model.Eventtypeofevent" “为以下类生成的空ID:com.iu.eblood.model.Eventtypeofevent类”

to set Id,i need to create an objejt for this EmbeddedId and when i create and set this Id, another problem appeared: 设置ID,我需要为此EmbeddedId创建对象,当我创建并设置此ID时,出现了另一个问题:

"The target name "EVENTID" is specified more than once for assignment in the same SQL statement" “在同一SQL语句中多次指定了目标名称“ EVENTID”以进行分配”

i guess,the reson for error is both my Eventtypeofevent object and EventtypeofeventPK object(class created by hibernate for EmbeddedId) actually show the same attribute. 我想,错误的原因是我的Eventtypeofevent对象和EventtypeofeventPK对象(由hibernate为EmbeddedId创建的类)实际上都显示相同的属性。

Here is my class: 这是我的课:

@Entity
public class Eventtypeofevent implements Serializable {

    private static final long serialVersionUID = 1L;
    private EventtypeofeventPK id;
    private Timestamp createddate;
    private Timestamp deleteddate;
    private String isactive;
    private Event event;
    private Eventtype eventtype;

    public Eventtypeofevent() {
    }


    @EmbeddedId
    public EventtypeofeventPK getId() {
        return this.id;
    }

    public void setId(EventtypeofeventPK id) {
        this.id = id;
    }
....... getter and setter

and other class for EmbeddedId: 和EmbeddedId的其他类:

    @Embeddable
public class EventtypeofeventPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;
    private long eventid;
    private int eventtypeid;
...... getter and setter

I used @EmbeddedId like this, I am not sure if it can help: 我像这样使用@EmbeddedId,我不确定它是否可以帮助您:

@EmbeddedId
@AttributeOverrides({
  @AttributeOverride(name = "orgId", column = @Column(name = "ORG_ID", nullable = false,     precision = 12, scale = 0)),
  @AttributeOverride(name = "configId", column = @Column(name = "CONFIG_ID", nullable = false, precision = 9, scale = 0)) })
  public [EmbeddedId Object] getId() {
    return id;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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