[英]@EmbeddedId with autogenerated id using @GenerateValue, this mix doesn't works as needed
我正在尝试使用@EmbeddedId,这是我的代码如下,
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)
)
Embedded类如下,
@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
...
}
员工实体如下,
@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
}
这是用main方法编写的代码,
private static void storVal(EntityManager em){
EmployeeEntitySix employee = new EmployeeEntitySix(new EmployeeIdTwo("KENYA"), "Henry Olaanga");
em.persist(employee);
}
但是一旦我运行上面的代码,我得到如下例外,
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Attempt to modify an identity column 'ID'.
如果我的EmbeddedId类包含自动生成的列,那么请告诉我出错的地方,而不是应该采用的方法。
只是要知道我使用hibernate作为持久性提供程序和JPA作为持久性API
我假设你不会问这个问题,如果Id
可以轻松而唯一地识别一名员工。 如果您能够修改表格,我建议使Id
可靠地唯一,或者添加自动生成的UniqueId
列。
其中一些错误实际上来自数据库架构,而不是来自JPA / Hibernate。 如果您尝试更新数据库没有生成策略的列,则可能是导致错误的原因。
您可能还会看一下使用JPA的autoincrement id没有反映在复合键中 ,因为我认为这涵盖了您所询问的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.