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.  [ 6 posts ] 
Author Message
 Post subject: Retrive child objects without parents
PostPosted: Thu Jul 06, 2006 10:20 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
Can anyone tell me if there is such a feature in NHibernate?

I have two associated objects lets name them Parent and Child. Child object has a reference to Parent object. Child object contains Id of Parent object in database, but objects are not linked with foreign key. My query fetches all Child objects from the table and also takes all Parent objects. All works fine if there is Parent object for each Children. But if the Parent object doesn't exist for any Child object then NHibernate generates exception. Is there is a way to make NHibernate assign null value to a Parent property of a Child object if Parent row doesn't exist for this Child without generating exception?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:26 am 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
In your Child mapping you probably have a many-to-one mapping for Parent. Set the not-null attribute to false.

Code:
<many-to-one name="Application" class="MobysThinkTank.mobyProject.Application, mobyProject" column="applicationId" not-null="false" />


Last edited by benhyrman on Thu Jul 06, 2006 11:18 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:36 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
benhyrman wrote:
In your Child mapping you probably have a many-to-one mapping for Parent. Set the not-null attribute to false.


No, it doesn't help :( I believe it isn't work because the value of a Parent Id column in a Child row isn't NULL but a specific not existing in Parent table number. Is there a way to make it work without changing all non-existing Id values to NULL?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 10:56 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, this feature is not present in NHibernate currently.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:03 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
sergey wrote:
No, this feature is not present in NHibernate currently.

Okay, I see. Actually I found a solution that helps me to do what I want without using triggers. I created a View which uses LEFT OUTER JOIN to merge tables and I can automatically put the value NULL to Parent Id column in all Child rows. But now I have a different problem, 'cause I had to change the value of a TABLE attribute in mapping file to table view. So now it is impossible for me to insert records of type Child. Can I somehow tell NHibernate that he must use my table view for fetching records and original table for inserting/updating?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 5:17 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
In the end I just created trigger, which nullify all reference to Parent table if someone deletes Parent row.


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