Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate使用触发器生成主键_Hibernate_Jpa_Jpa 2.0 - Fatal编程技术网

Hibernate使用触发器生成主键

Hibernate使用触发器生成主键,hibernate,jpa,jpa-2.0,Hibernate,Jpa,Jpa 2.0,我正在使用JPA2.0和Hibernate4 为了在表中生成主键,我使用表触发器。如果我是的话,扳机很好用 对主键使用Long-as数据类型。但是,如果我使用字符串作为主键 数据类型,然后我得到以下错误 org.springframework.orm.hibernate3.HibernateSystemException: Unknown integral data type for ids : java.lang.String; nested exception is org.hiberna

我正在使用JPA2.0和Hibernate4

为了在表中生成主键,我使用表触发器。如果我是的话,扳机很好用 对主键使用Long-as数据类型。但是,如果我使用字符串作为主键 数据类型,然后我得到以下错误

org.springframework.orm.hibernate3.HibernateSystemException: Unknown integral 
data type for ids : java.lang.String; nested exception is
org.hibernate.id.IdentifierGenerationException: Unknown integral
 data type for ids : java.lang.String at 
org.springframework.orm.hibernate3.SessionFactoryUtils.
convertHibernateAccessException(SessionFactoryUtils.java:690)
所以,不允许使用字符串作为主键来使用触发器生成值吗

使用触发器生成值的代码段

private String deptNo;
@Id
@GenericGenerator(name = "trig", strategy = "increment")
@GeneratedValue(generator = "trig")
@Column(name = "DEPT_NO")

  public String getDeptNo() {
    return deptNo;
}

public void setDeptNo(String deptNo) {
    this.deptNo = deptNo;
}

不允许在hibernate中为字符串使用生成的值,但是对键没有限制。如果您必须使用deptNo作为字符串,您可以为域对象创建一个适配器类,其中包含deptNo的长度为。

dursun使用字符串作为适配器类的确切含义是什么?你的意思是创建自定义类以生成键?不,我的意思是为当前类创建另一个对象,例如,如果当前类是Employee,您可以创建AdapterEmployee,它将Employees deptNo属性(长)转换为字符串。因此,当我写入数据库时,我应该调用AdapterEmployee类?您应该在需要deptNo作为字符串时使用adapter,在需要deptNo时使用实际类。因此,当您处理db时,您需要deptNo。