-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Strange behavior using Hibernate coupled to Unitils
PostPosted: Wed Dec 02, 2009 1:38 pm 
Newbie

Joined: Wed Dec 02, 2009 1:21 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Strange behavior using Hibernate coupled to Unitils
PostPosted: Thu Dec 03, 2009 5:25 am 
Newbie

Joined: Wed Dec 02, 2009 1:21 pm
Posts: 3
By making the relation bidirectionnal, the problem is solved.

However, this does not explain all the behavior, and this implies complex modifications into the source, in order to manage list and list parent.


Top
 Profile  
 
 Post subject: Re: Strange behavior using Hibernate coupled to Unitils
PostPosted: Thu Dec 03, 2009 6:03 am 
Newbie

Joined: Wed Dec 02, 2009 1:21 pm
Posts: 3
Using the following mapping solves all the problem :

<set name="emails" cascade="save-update">
<key column="contact_id" not-null="true" />
<one-to-many class="bo.contact.Email" />
</set>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.