-->
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.  [ 8 posts ] 
Author Message
 Post subject: Is it possible to cascade on <many-to-one> mapping?
PostPosted: Tue Sep 16, 2003 4:37 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Hello.

I have following tables:

----------------
T_LOCATION
location_id
street_address
city
country
----------------
T_PERSON
name
location_id
----------------
T_COMPANY
name
location_id
----------------

The idea is to just extract all location specific information into separate table. Every row in the location table used only once - either single Person or single Company points to it.

Using <many-to-one> for Location in the Person class works, but when new location is set for the Person or Company, it is not automatically saved (I have to call session.save(location) before session.save(person)). Is it possible to make save() operation cascade in this scenario? Also I need to delete record from T_LOCATION when Person's location is replaced with totally new. Actually, I need behavior much like collection with cascade="all-delete-orphan" but I do not want to use collection because it is known, there can be only one location for Company, not a set of locations.

Or are there other ways to implement what I want? Eliminating T_LOCATION table, putting all the properties into T_PERSON/T_COMPANY and using <component> is definitely not the best idea because:
1. There actually more than two entities which use location.
2. Some entities has two references to the location table (home_location_id and office_location_id for instance)
2. There are much more fields in the T_LOCATION table than I put in this sample.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 9:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can use all the cascade styles except "all-delete-orphan" on a <many-to-one>. However, there are usually extreme conceptual problems with cascade delete for a <many-to-one> (nothing to do with Hibernate).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 4:19 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Ok, I can not use all-delete-orphan. But what about cascade="all" ? Even if I set

cascade="all"
not-null="true"

for the "<many-to-one>" mapping from Person to Location and new Location to a Person and use session.save(person), I got non-null constraint violation (because Location is not saved before Person). What can I do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 4:23 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Oh, sorry, of course it is not about "non-null" constraing but about "foreign key does not exist" because when Person is added there is no location with specified location_id yet.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are mistaken. If you use cascade="save-update" or cascade="all", you should not see a FK violation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 4:54 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Heh. I have resolved the problem...
It was caused by absence of unsaved-value attribute for <id> elements - I was using primitive types (long instead of Long).

Strange enough...
Hibernate was retrieving nextval from LOCATION sequence before attempting to insert PERSON and that means it was going to save LOCATION anyway. But for some reasons it was planned after the insertion of PERSON. After unsaved-value attribute was added to both entities, everything works fine.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 4:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
A "long" is a "Long" and not primitive.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 6:29 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I mean I vas using

long id;

instead of

java.lang.Long id;

"long" IS primitive and can not be null :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.