@EmbeddedId使用@GenerateValue自动生成的id,这种混合不会按需要工作 [英] @EmbeddedId with autogenerated id using @GenerateValue, this mix doesn't works as needed

查看:494
本文介绍了@EmbeddedId使用@GenerateValue自动生成的id,这种混合不会按需要工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用@EmbeddedId,这是我的代码,如下所示:

  create table TBL_EMPLOYEE_002($ b $ (ID,COUNTRY)主键(ID,COUNTRY) 

嵌入式类如下,

  @Embeddable 
public class EmployeeIdTwo implements Serializable {
public EmployeeIdTwo(){} $ b $ public EmployeeIdTwo(String country){
this.empCountry = country;


@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =ID)
private Integer employeeId;

@Column(name =COUNTRY,length = 50)
private String empCountry;

//实现hashCode和equals和只有getters
...
}

employee实体如下,

  @Entity 
@Table(name = TBL_EMPLOYEE_002)
public class EmployeeEntitySix implements Serializable {
$ b $ public EmployeeEntitySix(){} $ b $ public EmployeeEntitySix(EmployeeIdTwo id,String name){
this.id = id ;
this.employeeName = name;
}

@EmbeddedId
private EmployeeIdTwo id;

@Column(name =NAME)
private String employeeName;

// getters and setters
}

这是代码写在main方法中,

  private static void storVal(EntityManager em){
EmployeeEntitySix employee = new EmployeeEntitySix(new EmployeeIdTwo(KENYA),Henry Olaanga);
em.persist(employee);
}

但是一旦我运行上面的代码,我得到如下的异常, p>

 原因:javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:尝试修改标识列'ID'。 

如果我的EmbeddedId类包含的话,您可以让我知道我出错的地方,
一个自动生成的列,比应该是这种方法。



只需要知道我使用hibernate作为持久性提供程序和JPA作为持久性API


<如果 Id 可以轻松唯一地识别员工,我假设您不会问这个问题。如果你有修改你的表的能力,我会建议让 Id 可靠唯一,或者添加一个自动生成的 UniqueId 列。

其中一些类型的错误实际上来自数据库模式,而不是来自JPA / Hibernate。如果您尝试更新数据库没有生成策略的列,则可能是导致错误的原因。



您可能还有一个请查看 autoincrement id并未反映在使用JPA的组合键中,因为我认为这涵盖了您询问的内容。


I am trying to work with @EmbeddedId, this is my code as follows,

create table TBL_EMPLOYEE_002(
  ID integer generated always as identity (start with 100,increment by 10), 
  COUNTRY varchar(50),
  NAME varchar(50),
  constraint PK_EMP_00240 primary key(ID,COUNTRY)
)

The Embedded class as follows,

@Embeddable
public class EmployeeIdTwo implements Serializable{
     public EmployeeIdTwo(){}
    public EmployeeIdTwo(String country){
        this.empCountry = country;
    }

    @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name="ID") 
    private Integer employeeId;

    @Column(name="COUNTRY",length=50)
    private String empCountry;

// implementation of hashCode and equals and only getters 
...
}

employee Entity as follows,

@Entity
@Table(name="TBL_EMPLOYEE_002")
public class EmployeeEntitySix implements Serializable{

    public EmployeeEntitySix(){}
    public EmployeeEntitySix(EmployeeIdTwo id,String name){
        this.id = id;
        this.employeeName = name;
    }

    @EmbeddedId    
    private EmployeeIdTwo id;

    @Column(name="NAME")
    private String employeeName;

// getters and setters
}

this is the code written in main method,

private static void storVal(EntityManager em){
    EmployeeEntitySix employee = new EmployeeEntitySix(new EmployeeIdTwo("KENYA"), "Henry Olaanga");
    em.persist(employee);
}

but once i run an above code i get an exception as follows,

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Attempt to modify an identity column 'ID'. 

Can you please let me know where i am getting wrong, if my EmbeddedId class contain an autogenerated column, than what should be the approach.

Just to know I am using hibernate as persistence provider and JPA as persistence API

解决方案

I'm assuming you would not be asking this question if Id could easily and uniquely identify an employee. If you have the ability to modify your tables, I'd suggest either making Id reliably unique, or adding an auto-generated UniqueId column.

Some of these kinds of errors actually come from the DB schema, and not from JPA/Hibernate. If you're trying to update a column that the DB doesn't have a generation strategy for, this might be what is causing your error.

You might also have a look at autoincrement id is not reflecting in composite key using JPA, as I think this covers what you are asking about.

这篇关于@EmbeddedId使用@GenerateValue自动生成的id,这种混合不会按需要工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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