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: Removing entities
PostPosted: Mon Nov 27, 2006 12:24 pm 
Newbie

Joined: Mon Nov 27, 2006 11:49 am
Posts: 2
I have this problem. Let's have two entities, for example Car and Person. One person can own n cars, a car can be owned by one person, so it's one-to-many association. The association is unidirectional, you can obtain owner from
Code:
Person Car.getOwner()
, but you can't get list of cars from Person.

And now here's the problem. I need to remove Car and Person entities in this way:

1, when you remove a Car, his owner is not removed.
2, when you remove a Person, all his cars are removed.

I was able to solve the first point by simply removing CascadeType from the @ManyToOne annotation, so the removing of a Car doesn't remove the owner.


But I can't solve the second point, I can't add CascadeType.REMOVE to the Person class, because the association is unidirectional, so I dont use @OneToMany annotation there.

Thanks for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:31 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Why your link must be unidirectional?

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:58 pm 
Newbie

Joined: Mon Nov 27, 2006 11:49 am
Posts: 2
andresgr wrote:
Why your link must be unidirectional?


I wanted to know, if there is a way to do it with just a unidirectional association. I don't need the bidirectional one in my application.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:02 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Hmm...

1) Have your database do it using a cascading foreign key constraint.
2) Use <sql-delete> in your class mapping. Never tried this, but I suppose it might work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:07 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
If the only reason for the unidirectionality is "curiosity" i suggest you to go with the bidirectional thing, it will give you more flexibility in the future... And you'll get the cascade actions easily and naturally... so you can use hbm2ddl without messing with the DB schema yourself.

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject: Re: Removing entities
PostPosted: Fri Sep 04, 2009 3:31 pm 
Newbie

Joined: Fri Sep 04, 2009 3:13 pm
Posts: 3
can anybody help to solve this problem?
Not a curiosity. If I use bidirectional I'll have lots of dummy properties in classes %(


Top
 Profile  
 
 Post subject: Re: Removing entities
PostPosted: Fri Sep 04, 2009 7:20 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What about a delete orphan cascade type?

_________________
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: Re: Removing entities
PostPosted: Fri Sep 11, 2009 11:31 am 
Newbie

Joined: Fri Sep 04, 2009 3:13 pm
Posts: 3
DELETE_ORPHAN is a OneToMany option which does nothing for ManyToOne. isn't it?


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.