-->
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.  [ 4 posts ] 
Author Message
 Post subject: ManytoOne not working
PostPosted: Fri Feb 12, 2010 4:11 pm 
Newbie

Joined: Fri Feb 12, 2010 3:55 pm
Posts: 2
Why doesnt the following work

versions
hibernate-annotations-3.4.0.GA
hibernate-distribution-3.3.2.GA
hibernate-entitymanager-3.4.0.GA
hibernate-validator-4.0.1.GA

mysql-5.1.41-linux-i686-glibc23

I have entities as follows: Person, Gender, Title

Person has gender_id and title_id as foreign keys, with foreign indexes set up to gender and title tables

The only way i can insert a Person row is to populate persons Gender and Title attributes with rows read
from the Gender and Title tables.

If I instantiate Gender and Title objects and assign these to person, the save fails with

org.hibernate.PropertyValueException: not-null property references a null or transient value: com.pre.school.dao.Person.gender


Top
 Profile  
 
 Post subject: Re: ManytoOne not working
PostPosted: Fri Feb 12, 2010 5:42 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
That is what a foreign key means. The person must reference a gender and title that is present in the database (unless null values are allowed). But, assuming that you also want to create new entries in the Gender and Title tables, there are two ways to go:

1. Save the new Gender and Title objects in your own code.
2. Setup cascading so that Hibernate automatically saves them to the database. Eg. map your @ManyToOne:s with cascade=CascadeType.PERSIST. See http://docs.jboss.org/hibernate/stable/ ... ml#d0e1177 for some examples.


Top
 Profile  
 
 Post subject: Re: ManytoOne not working
PostPosted: Sat Feb 13, 2010 4:18 am 
Newbie

Joined: Fri Feb 12, 2010 3:55 pm
Posts: 2
Thanks for looking at this...

However what I am trying to do is use the gender and title as read only reference tables (So I do not want to insert new rows , or indeed delete rows from them when i delete an owning person row)

I just want to have a many link from all my Person(s) to each one Gender and or Title row.

I think the problem is that the gender and title entities are not associated with the hibernate session


Top
 Profile  
 
 Post subject: Re: ManytoOne not working
PostPosted: Sat Feb 13, 2010 1:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Use Session.load() to get the Gender and Title instances. This will not hit the database unless you have disabled proxies. Then use those instances in the many-to-one associations on the new Person.


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