PostgreSQL在JPA单表分层结构中的标识 [英] PostgreSQL identity in JPA single table hierarchy
问题描述
首先是我的环境:
$ b在一个Hibernate / JPA环境中使用PostgreSQL$ b
- PostgreSQL 8.3
- Spring 2.5.6SEC01
- Hibernate-entitymanager 3.4.0 .GA(从Glassfish更新工具安装)
- Hibernate-distribution 3.3.1.GA(从Glassfish更新工具安装)
- Glassfish V2
-
- Mac OS X 10.5.x
使用<$ c时遇到问题$ c> GenerationType IDENTITY
与单个表继承层次结构。
这里是我的两个实体:
$ b $ p Parent.java
<$ c $ b $ @ @Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =generation,discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(Parent)
public class Parent实现Serializable {
private static final long serialVersionUID = 1L;
私人长ID;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
}
Child.java
@Entity
@DiscriminatorValue(Child)
public class Child extends Parent {
私人字符串babyName;
$ b $ **
* @return the babyName
* /
public String getBabyName(){
return babyName;
$ b $ **
* @param babyName要设置的babyName
* /
public void setBabyName(String babyName){
this.babyName = babyName;
DDL
create table父(生成varchar(31)不为空
,id bigserial不为空
,babyName varchar(255 )
,主键(id)
);
如果我尝试插入新的父项,则会收到错误消息:
org.postgresql.util.PSQLException:类型long的值不正确:父
我把日志记录到TRACE,这是输出:
TRACE TransactionSynchronizationManager - 限制值[org.springframework.orm.jpa.EntityManagerHolder@485e0366]键[com.sun.enterprise.util.EntityManagerFactoryWrapper@a6e312b]为线程[httpSSLWorkerThread-8080-0]
Hibernate:插入父(生成)值('Parent')
SQL错误:0,SQLState:22003
类型long的值不正确:父
TRACE TransactionInterceptor - 完成[com.xxx.yyy.service.MoveService.saveHuman ]异常后:javax.persistence.PersistenceException:org.hibernate.exception.DataException:无法插入:[com.xxx.yyy.temp.Parent]
TRACE RuleBasedTransactionAttribute - 应用规则来确定事务sh回滚javax.persistence.PersistenceException:org.hibernate.exception.DataException:无法插入:[com.xxx.yyy.temp.Parent]
如果我将标识符值更改为长分析的内容,例如 123
插入成功。不过,Hibernate似乎认为这个鉴别值是id。所以下面的代码:
父p = new Parent();
service.saveHuman(p);
add(new Label(p,p id is+ p.getId()));
显示id为 123
,而不是主键值为1。如果我将生成类型更改为 AUTO
,则会创建单个序列,并且插入似乎工作正常。如果我将它更改为 SEQUENCE
,并再次使用类级注释声明序列,它就可以工作。然而,根据我已阅读的文档,PostgreSQL支持这两种生成类型。
我也尝试了完全相同的代码,并用MySQL 5.0.51b转出数据库。这似乎运作良好。
那么这是我的代码,方言,Hibernate或PostgreSQL JDBC驱动程序中的错误?任何想法如何我可以缩小这个问题?任何人都可以重新产生这个问题,因为我试图搜索,还没有看到这个确切的问题。
注意:我也问过关于 Hibernate论坛的问题。当然我会反馈任何回应到这两个网站。
我只是从上面的注释中移植这个解决方案。 。使用版本8.3的PostgreSQL JDBC驱动程序: - )。
I'm seeing strange behaviour when using PostgreSQL in a Hibernate/JPA environment with a single table inheritance hierarchy.
Firstly my environment:
- PostgreSQL 8.3
- Spring 2.5.6SEC01
- Hibernate-entitymanager 3.4.0.GA (installed from Glassfish update tool)
- Hibernate-distribution 3.3.1.GA (installed from Glassfish update tool)
- Glassfish V2
- Mac OS X 10.5.x
I'm having an issue when using a GenerationType
of IDENTITY
with a single table inheritance hierarchy.
Here are my two entities:
Parent.java
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="generation", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("Parent")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Child.java
@Entity
@DiscriminatorValue("Child")
public class Child extends Parent {
private String babyName;
/**
* @return the babyName
*/
public String getBabyName() {
return babyName;
}
/**
* @param babyName the babyName to set
*/
public void setBabyName(String babyName) {
this.babyName = babyName;
}
}
DDL
create table Parent (generation varchar(31) not null
,id bigserial not null
,babyName varchar(255)
,primary key (id)
);
If I try to insert a new Parent I get an error:
org.postgresql.util.PSQLException: Bad value for type long : Parent
I turned up the logging to TRACE and this is the output:
TRACE TransactionSynchronizationManager - Bound value [org.springframework.orm.jpa.EntityManagerHolder@485e0366] for key [com.sun.enterprise.util.EntityManagerFactoryWrapper@a6e312b] to thread [httpSSLWorkerThread-8080-0] Hibernate: insert into Parent (generation) values ('Parent') SQL Error: 0, SQLState: 22003 Bad value for type long : Parent TRACE TransactionInterceptor - Completing transaction for [com.xxx.yyy.service.MoveService.saveHuman] after exception: javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not insert: [com.xxx.yyy.temp.Parent] TRACE RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not insert: [com.xxx.yyy.temp.Parent]
If I change the discriminator value to be something that parses as a long e.g. 123
the insert succeeds. However Hibernate seems to think this discriminator value is the id. So the following code:
Parent p = new Parent();
service.saveHuman(p);
add(new Label("p", "p id is" + p.getId()));
Shows the id as 123
rather than the primary key value which is 1.
If I change the generation type to AUTO
a single sequence is created and the insert seems to work ok. If I change it to SEQUENCE
and declare the sequence with a class-level annotation again it works. However according to the documentation I have read PostgreSQL supports both generation types.
I also tried the exact same code and switched out the database with MySQL 5.0.51b. This seems to function fine.
So is this a bug in my code, the dialect, Hibernate, or the PostgreSQL JDBC driver? Any ideas how I can narrow down the problem? Anyone able to re-produce this issue as I have tried to search and haven't seen this exact problem yet.
Note: I've also asked the question on the Hibernate Forums. Naturally I will feedback any responses to both sites.
I'm just "porting" this solution from the comments above... use version 8.3 of the PostgreSQL JDBC driver :-).
这篇关于PostgreSQL在JPA单表分层结构中的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!