Note that there are some explanatory texts on larger screens.

plurals
  1. POPostgreSQL identity in JPA single table hierarchy
    primarykey
    data
    text
    <p>I'm seeing strange behaviour when using PostgreSQL in a Hibernate/JPA environment with a single table inheritance hierarchy.</p> <p>Firstly my environment:</p> <ul> <li>PostgreSQL 8.3</li> <li>Spring 2.5.6SEC01</li> <li>Hibernate-entitymanager 3.4.0.GA (installed from Glassfish update tool)</li> <li>Hibernate-distribution 3.3.1.GA (installed from Glassfish update tool)</li> <li>Glassfish V2</li> <li>Mac OS X 10.5.x</li> </ul> <p>I'm having an issue when using a <code>GenerationType</code> of <code>IDENTITY</code> with a single table inheritance hierarchy.</p> <p>Here are my two entities:</p> <p><strong>Parent.java</strong></p> <pre><code>@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; } } </code></pre> <p><strong>Child.java</strong></p> <pre><code>@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; } } </code></pre> <p><strong>DDL</strong></p> <pre><code>create table Parent (generation varchar(31) not null ,id bigserial not null ,babyName varchar(255) ,primary key (id) ); </code></pre> <p>If I try to insert a new Parent I get an error:</p> <pre><code>org.postgresql.util.PSQLException: Bad value for type long : Parent </code></pre> <p>I turned up the logging to TRACE and this is the output:</p> <pre> 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] </pre> <p>If I change the discriminator value to be something that parses as a long e.g. <code>123</code> the insert succeeds. However Hibernate seems to think this discriminator value is the id. So the following code:</p> <pre><code>Parent p = new Parent(); service.saveHuman(p); add(new Label("p", "p id is" + p.getId())); </code></pre> <p>Shows the id as <code>123</code> rather than the primary key value which is 1.</p> <p>If I change the generation type to <code>AUTO</code> a single sequence is created and the insert seems to work ok. If I change it to <code>SEQUENCE</code> 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.</p> <p>I also tried the exact same code and switched out the database with MySQL 5.0.51b. This seems to function fine.</p> <p>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.</p> <p><strong>Note:</strong> I've also asked the question on the <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=999196" rel="nofollow noreferrer">Hibernate Forums</a>. Naturally I will feedback any responses to both sites.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload