I have a simple subclass mapping:
Code:
<class name="com.nimblefish.core.data.Task">
<id name="id" type="long" unsaved-value="null" >
<generator class="native"/>
</id>
<discriminator/>
<!-- some properties elided -->
<subclass name="com.nimblefish.core.data.TaskImport"
extends="com.nimblefish.core.data.Task"
discriminator-value="IMP">
<many-to-one name="sourceList" column="sourcelist_id" not-null="true"
class="com.nimblefish.core.data.SourceList"/>
</subclass>
</class>
If I try to create an instance of Task, which *has* no sourceList property, I get:
Code:
<error message="could not insert: [com.nimblefish.core.data.Task]" type="net.sf.hibernate.JDBCException">net.sf.hibernate.JDBCException: could not insert: [com.nimblefish.core.data.Task]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:541)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:433)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:876)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:817)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:737)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at com.nimblefish.core.test.persist.PersistenceManagerTest.testTask(PersistenceManagerTest.java:210)
...
Caused by: java.sql.SQLException: General error, message from server: "Cannot add or update a child row: a foreign key constraint fails"
I assume this means that if you are using a table-per-class-hierarchy mapping, you can't have not-null subclass properties, since instances of other classes in the hierarchy *will* have null values for those properties? (Removing the not-null on the "sourceList" subclass property makes the problem stop happening.)
This is with Hibernate 2.1.1.
Thanks for any confirmation :-)
Cheers!
Rob