-->
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.  [ 2 posts ] 
Author Message
 Post subject: Need Help With Unit Tests
PostPosted: Sat Jul 08, 2006 8:56 am 
Newbie

Joined: Sat Jul 08, 2006 8:44 am
Posts: 6
I'm new to Hibernate and I'm trying to get some basic unit tests working. I am getting a "not-null property references a null or transient value" error message.

I have two domain objects: Pet and Category. Pet can belong to at most one category. I have two corresponding DAO's: PetDao and CategoryDao.

In PetDaoTest I create a transient Category and call setCategory on the Pet object. Then I'm trying to save the Pet object. I think Hibernate is complaining b/c there is no FK for the Category object since its not persistent and there is no cascade from the category property. (Please confirm.)

So my question is more of a design question. How can I have an effective unit test that isolates the PetDao without involving CategoryDao? Does it make sense to have the two separate DAO's at all? If so, does it make sense to test them separately?

Regards,

Sean


<class name="Pet">
<id name="id"
column="pet_id"
type="long"
unsaved-value="0">
<generator class="native"/>
</id>
<property name="description"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one
name="category"
column="category_id"
class="Category"
not-null="true"/>
</class>

<class name="Category">
<id name="id"
column="CATEGORY_ID"
type="long"
unsaved-value="0">
<generator class="native"/>
</id>
<property name="description"/>
<property name="parentCategory"/>
<set name="pets"
inverse="true"
cascade="save-update">
<key column="category_id"/>
<one-to-many class="Pet"/>
</set>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 12:18 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate is complaining because it must have a category ID to save into the category_id column of the pet table, but you have no ID to save.

To work around this without using CategoryDAO, have a precondition for you tests be that there must be a row in the category table with ID = 1, and that any category object you create for unit testing purposes must have ID 1. Hibernate will match your transient category object with the dummy unit test category row, and all will be well.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.