I'm just starting out with Hibernate, however, I've come across a problem relating to persisting my MailboxesEntity class to the database. I'm currently using hibernate-2.0.3, and MySQL with primary_key as auto_increment.
I have listed the hibernate mappings for both classes and the hibernate DEBUG. It seems that when I create a brand new (Transient) CustomersEntity class (initialising all properties except for the customer_id which is set to NULL) and attempt to persist it to the database, it fails.
From the DEBUG, hibernate seems to be happy storing the CustomerEntity class, it manages to perform a SELECT LAST_INSERT_ID() and retrieve the customer_id (shown in red), however when it stores this ID as a foreign key to the MailboxesEntity class, it is using NULL (show in red).
Why is hibernate not storing the correct customer_id value as a foreign key to the MailboxesEntity class?
Appreciate any help or pointers!!
Code:
<hibernate-mapping>
<class
name="uk.co.blueyonder.selfcareSS.model.CustomersEntity"
table="customers"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="customer_id"
column="customer_id"
type="integer"
>
<generator class="native">
</generator>
</id>
<property
name="mailboxname"
type="string"
update="true"
insert="true"
column="mailboxname"
/>
<property
name="active"
type="char"
update="true"
insert="true"
column="active"
/>
<set
name="mailboxes"
table="mailboxes"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="customer_id"
/>
<one-to-many
class="uk.co.blueyonder.selfcareSS.model.MailboxesEntity"
/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="uk.co.blueyonder.selfcareSS.model.MailboxesEntity"
table="mailboxes"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="mailbox_id"
column="mailbox_id"
type="integer"
>
<generator class="native">
</generator>
</id>
<property
name="username"
type="string"
update="true"
insert="true"
column="username"
/>
<property
name="customer_id"
type="integer"
update="true"
insert="true"
column="customer_id"
not-null="true"
/>
<set
name="preferences"
table="preferences"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="mailbox_id"
/>
<one-to-many
class="uk.co.blueyonder.selfcareSS.model.PreferencesEntity"
/>
</set>
<set
name="aliases"
table="aliases"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="mailbox_id"
/>
<one-to-many
class="uk.co.blueyonder.selfcareSS.model.AliasesEntity"
/>
</set>
</class>
</hibernate-mapping>
DEBUG JDBCTransaction - begin
DEBUG Cascades - unsaved-value strategy NULL
DEBUG SessionImpl - saveOrUpdate() unsaved instance with id: null
DEBUG SessionImpl - saving [uk.co.blueyonder.selfcareSS.model.CustomersEntity#<null>]
DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
DEBUG Cascades - done processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.CustomersEntity.mailboxes
DEBUG EntityPersister - Inserting entity: uk.co.blueyonder.selfcareSS.model.CustomersEntity (native id)
DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG SessionFactoryImpl - prepared statement get: insert into customers (mailboxname, active) values (?, ?)
DEBUG SessionFactoryImpl - preparing statement
DEBUG EntityPersister - Dehydrating entity: uk.co.blueyonder.selfcareSS.model.CustomersEntity#null
DEBUG StringType - binding 'bnwk01010' to parameter: 1
DEBUG CharacterType - binding 'Y' to parameter: 2
DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG SessionFactoryImpl - closing statement
DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG SessionFactoryImpl - prepared statement get: SELECT LAST_INSERT_ID()
DEBUG SessionFactoryImpl - preparing statement
DEBUG EntityPersister - Natively generated identity: 2
DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG SessionFactoryImpl - closing statement
DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.CustomersEntity
DEBUG Cascades - cascading to collection: uk.co.blueyonder.selfcareSS.model.CustomersEntity.mailboxes
DEBUG Cascades - cascading to saveOrUpdate()
DEBUG Cascades - unsaved-value strategy NULL
DEBUG SessionImpl - saveOrUpdate() unsaved instance with id: null
DEBUG SessionImpl - saving [uk.co.blueyonder.selfcareSS.model.MailboxesEntity#<null>]
DEBUG Cascades - processing cascades for: uk.co.blueyonder.selfcareSS.model.MailboxesEntity
DEBUG Cascades - done processing cascades for: uk.co.blueyonder.selfcareSS.model.MailboxesEntity
DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.MailboxesEntity.preferences
DEBUG SessionImpl - Wrapped collection in role: uk.co.blueyonder.selfcareSS.model.MailboxesEntity.aliases
DEBUG EntityPersister - Inserting entity: uk.co.blueyonder.selfcareSS.model.MailboxesEntity (native id)
DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG SessionFactoryImpl - prepared statement get: insert into mailboxes (username, customer_id) values (?, ?)
DEBUG SessionFactoryImpl - preparing statement
DEBUG EntityPersister - Dehydrating entity: uk.co.blueyonder.selfcareSS.model.MailboxesEntity#null
DEBUG StringType - binding 'bnwk01010_2' to parameter: 1
DEBUG IntegerType - binding null to parameter: 2
DEBUG JDBCExceptionReporter - SQL Exception
java.sql.SQLException: General error: Column 'customer_id' cannot be null
[/code]