Hi,
I'm using @Inheritance annotation with InheritanceType.JOINED startegy and I have a strange bug when I try to insert an object.
I use this simple mapping :
Code:
@Entity
@Table(name = "tb_foo")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Foo {
@Id
@Column(name = "ID_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
}
Code:
@Entity
@Table(name = "tb_bar")
@PrimaryKeyJoinColumn(name = "Foo_ID")
public class Bar extends Foo {
@Column(name = "val")
private String val;
}
When I just perform a save :
Code:
Session session = new Configuration().configure(resource).buildSessionFactory().openSession();
session.beginTransaction();
Bar bar = new Bar();
session.save(bar);
session.getTransaction().commit();
I have the error :
Code:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [fr.viveris.vizada.box.tests.Bar]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:63)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2346)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2853)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at fr.viveris.vizada.box.tests.Bar.main(Bar.java:25)
Caused by: org.postgresql.util.PSQLException: The column name Foo_ID was not found in this ResultSet.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.findColumn(AbstractJdbc2ResultSet.java:2728)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2594)
at org.hibernate.id.IdentifierGeneratorHelper.get(IdentifierGeneratorHelper.java:145)
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:88)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:97)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:56)
... 16 more
I suspect that hibernate first insert data in table tb_foo and then search the generated id to do the insertion in tb_bar. But it seems that hibernat trying to get the ID using the field 'Foo_ID' instead of using the field 'ID_ID'.
Maybe I'm doing something wrong ?
I use Postgresql as database with these tables :
Code:
create table IF NOT EXISTS tb_foo (
ID_ID bigserial not null primary key,
name varchar
);
create table IF NOT EXISTS tb_bar (
Foo_ID bigint not null primary key,
val varchar,
FOREIGN KEY (Foo_ID) REFERENCES tb_foo (ID_ID)
);
I use hibernate in version 3.6