Hello,
I am developping JUnit/Unitils tests in order to validate my business rules. Due to different reasons, I want to enable the constraints on the database, which is not the default Unitils behavior.
Since this modification has been done, I obtain the following exception on different tests :
18:25:12.417 [ERROR] AddEmailTestCase - Unexpected exception architecture.exceptions.explicit.DataSourceException: An exception has occurred when trying to load an entity [bo.contact.Contact] with the identifier [1] from the database. Caused by: org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [bo.contact.Email]; uncategorized SQLException for SQL [insert into email (email_from, email_to, body, subject, template_id, template_version, creation_date, email_type_id) values (?, ?, ?, ?, ?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'contact_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'contact_id' doesn't have a default value [...] Caused by: java.sql.SQLException: Field 'contact_id' doesn't have a default value
The exact process for this case is the following :
1 - Finding the Contact [1] in the database, 2 - Deleting the existing email on the Contact's emails list, 3 - Adding a new email on the Contact's emails list, 4 - Updating the Contact in order to propagate the modifications.
The last action always performs a verification on the database : it verifies if the Contact exists by performing a get on the HibernateDAOSupport class.
What I understand :
- While trying to update the contact, Hibernate tries to insert the email, - The email does not correctly reference the Contact (the foreign key is null), - The insertion fails.
I understand why the insertion fails, but I do not understand why Hibernate tries to insert the email while trying to get a Contact. It should do it on the next step.
This is my Hibernate mapping to link the email list on the contact:
<set name="emails" cascade="save-update"> <key column="contact_id" /> <one-to-many class="bo.contact.Email" /> </set>
Thanks per advance to anyone, Thomas
|