2009-08-26 2 views
2

단일 테이블 상속 계층이있는 Hibernate/JPA 환경에서 PostgreSQL을 사용할 때 이상한 동작이 나타납니다. 첫째JPA 단일 테이블 계층 구조의 PostgreSQL ID

내 환경 :

  • PostgreSQL의 8.3
  • 봄 2.5.6SEC01
  • 최대 절전 모드-의 EntityManager의 3.4.0.GA (글래스 피시 업데이트 도구에서 설치)
  • 최대 절전 모드 분포 3.3.1 .GA (Glassfish 업데이트 도구에서 설치)
  • 글래스 피쉬 V2
  • Mac OS X 10.5.x

GenerationTypeIDENTITY이고 하나의 테이블 상속 계층 구조를 사용할 때 문제가 있습니다.

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

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; 
    } 
} 

: 여기

내 두 엔티티

org.postgresql.util.PSQLException: Bad value for type long : Parent 

내가 추적 할 로깅을 설정하고이 출력입니다 :

create table Parent (generation varchar(31) not null 
        ,id bigserial not null 
        ,babyName varchar(255) 
        ,primary key (id) 
        ); 

나는 새로운 부모를 삽입하려고하면 오류가 발생합니다 내가 판별 값을 변경하는 경우

 
TRACE TransactionSynchronizationManager - Bound value [[email protected]] for key [[email protected]] 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] 

예를 들어 긴 것으로 해석하는 어떤 것 123 삽입이 성공합니다. 그러나 Hibernate는이 discriminator 값이 id라고 생각하는 것 같습니다. 그래서 다음 코드를

Parent p = new Parent(); 
service.saveHuman(p); 

add(new Label("p", "p id is" + p.getId())); 

오히려 내가 AUTO 하나의 시퀀스를 생성에 발생 유형을 변경하고 삽입이 작동하는 것 같다 경우 1

있는 기본 키 값보다 123으로 ID를 표시합니다 승인. SEQUENCE으로 변경하고 클래스 수준 주석으로 시퀀스를 다시 선언하면 다시 작동합니다. 그러나 내가 읽은 문서에 따르면 PostgreSQL은 두 가지 생성 유형을 모두 지원합니다.

또한 동일한 코드를 시도하고 MySQL 5.0.51b를 사용하여 데이터베이스를 전환했습니다. 이것은 잘 작동하는 것 같습니다.

내 코드, dialect, Hibernate 또는 PostgreSQL JDBC 드라이버의 버그입니까? 어떻게하면 문제를 좁힐 수 있을지 모든 아이디어? 검색을 시도했지만 아직 정확한 문제를 보지 못한 상태에서이 문제를 재현 할 수있는 사람.

참고 : 나는 또한 Hibernate Forums에 대한 질문을했습니다. 당연히 두 사이트에 대한 모든 의견을 피드백으로 보내 드리겠습니다.

+0

어떤 버전의 PostgreSQL JDBC 드라이버를 사용하고 있습니까? –

+0

그것은 postgresql-8.4-701.jdbc4.jar입니다. 좋은 지적입니다. 차이가 있는지 확인하기 위해 다른 버전을 사용해 보겠습니다. – Stephen

+0

@Matt Solnit 그냥 postgresql-8.3-605.jdbc4 해봤지만 제대로 작동합니다! 매우 감사합니다. 이 질문을 답으로 쓰고 싶으면 내가 배정하겠습니다. – Stephen

답변

1

위의 주석에서이 솔루션을 "이식"하고 있습니다. PostgreSQL JDBC 드라이버 버전 8.3을 사용하십시오 :-).