-->
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.  [ 5 posts ] 
Author Message
 Post subject: JoinColumn not-nullable for manyToOne and oneToOne
PostPosted: Wed Jul 16, 2008 5:11 am 
Newbie

Joined: Mon Jul 14, 2008 4:52 pm
Posts: 6
Location: London
Hibernate version: 3.2.6

Mapping documents: annotation

Name and version of the database you are using: postgres 8


Hi everyone,

I'm mapping an existing dataBase to Hibernate and I have an issue with manyToOne or even OneToOne.

On the db schema, the joinColumn for all our relations are defined as NOT NULLABLE, so 0 (zero) set a relation to null.

How can I tell hibernate that 0 means there is no relation ? (because today hibernate tries to find the relation and return an empty object).

If I use "@NotFound(action = NotFoundAction.IGNORE)" it works but I loose integrity constraints.

I've seen Formula and Filter but I don't realy know which one to use.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:05 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This question comes up from time to time in the Hibernate forums.

According to Gavin King, one of the men behind Hibernate, the way to do this is to map 0 to null is to write a UserType that transforms 0 to null, and use that custom UserType as the identified type. Hibernate will not create a schema with default values, so in this case, you're on your own.

Another neat Hibernate trick is to use the nullif function in HQL. It's slick:

from Clazz c where nullif(c.cat, 0) is null

If both expressions are equal, null is returned, otherwise the first expression is returned, as you would expect. This simply helps out in the coding for the null or zero values.

This was discussed by men much smarter and better looking than me in the following thread:

http://forum.hibernate.org/viewtopic.php?t=945594

I'm also maintaining a list of suggestions and ideas on the topic on my blog:

http://hibernate3tutorials.blogspot.com/2008/07/how-to-map-integer-column-value-of-0.html

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:17 am 
Newbie

Joined: Mon Jul 14, 2008 4:52 pm
Posts: 6
Location: London
Hi Cameron,

Thanks for you quick response.

I think I'll start using "not-found="ignore".

I might also spend time writing a generic UsertType since all the relations I have are using the same "not nullable" join column.

Cheers,
Sebastien


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 16, 2008 6:19 am 
Newbie

Joined: Mon Jul 14, 2008 4:52 pm
Posts: 6
Location: London
Sorry, error while posting !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 11:08 am 
Newbie

Joined: Mon Jul 14, 2008 4:52 pm
Posts: 6
Location: London
What about using Formula ?

Would that be an alternative ?


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