I have a similar issue. I have a parent table with several child tables. Subcomponent is the parent. Term is one of the children. The parent and the children all have a CREATED_DATE column.
Two tables:
--------------------------
SUBCOMPONENT
--------------------------
SUBCOMPONENT_ID (PK)
SUBCOMPONENT_TYPE
CREATE_DATE
--- more columns
--------
TERM
--------
SUBCOMPONENT_ID (PK) (FK)
CREATE_DATE
--- more columns
Code:
@Entity
@Table(name = "PRM_SUBCOMPONENT")
@Inheritance(strategy = InheritanceType.JOINED)
public class Subcomponent implements Serializable {
Date createdDate
...
}
@Entity
@Table(name = "PRM_TERM")
@PrimaryKeyJoinColumn(name = "SUBCOMPONENT_ID")
@DiscriminatorColumn(name = "SUBCOMPONENT_TYPE", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("TERM")
public class Term extends Subcomponent implements Serializable {
...
}
Now if I do the following:
Code:
Subcomponent subcomponent = new Subcomponent();
subcomponent .setCreatedDate(new Date);
...
subcomponent Dao.save(subcomponent );
Term term = new Term();
term.setCreatedDate(new Date);
...
termDao.update(term);
the subcomponent persists fine, but and the term starts saving correctly (I see that Hibernate first INSERTS a subcomponent and then a term but I get a SQL exception: cannot insert NULL into ("PRM"."PRM_TERM"."CREATED_DATE")
The work around I've found is to define a new field in the Term class called termCreatedDate and map that to the CREATED_DATE column on the PRM_TERM table. This seems to defeat the purpose of using inheritance. I'm using Hibernate 3.2.2 (latest version) as the JPA provider. Any ideas anyone?
Hibernate version:
3.2.2
Mapping documents:
Name and version of the database you are using:
Mysql 5.0.X
The generated SQL (show_sql=true):
2007-03-16 13:26:19,110 [main] DEBUG org.hibernate.SQL - insert into PRM_SUBCOMPONENT (SUBCOMPONENT_TYPE, SUBCOMPONENT_ID, more columns...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2007-03-16 13:26:19,110 [main] DEBUG org.hibernate.SQL - insert into PRM_TERM (SUBCOMPONENT_ID, more columns...) values (?, ?, ?, ?)