Hi,
Firstly my environment:
PostgreSQL 8.3
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's my two entities:
Parent.javaCode:
@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;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Parent)) {
return false;
}
Parent other = (Parent) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Parent[id=" + id + "]";
}
}
Child.javaCode:
@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;
}
}
DDLCode:
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:
Code:
org.postgresql.util.PSQLException: Bad value for type long : Parent
I turned up the logging to TRACE and this is the output:
Code:
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:
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 it seems to work ok. However I don't want a single sequence across all my tables.
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.
Many thanks
Stephen