-->
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.  [ 7 posts ] 
Author Message
 Post subject: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Thu Mar 17, 2011 9:38 pm 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Hello,

I have many-to-one relation, where A can have many B entities, and B can have only one A.

I define annotation in A like:

@OneToMany(targetEntity=B.class, fetch=FetchType.LAZY, mappedBy="blah", cascade=CascadeType.ALL)
@Column(name="someID", nullable=false) // inverse=true
private Set<B> childs = new HashSet<B>();

In B i have something like this:

@ManyToOne(targetEntity=A.class, fetch=FetchType.LAZY)
@JoinColumn(name="someID", referencedColumnName="...", nullable=false)
private A a;

When i add new B entities in childs set, or modify some entity in set and save A everything propagates well, and
B updates in database properly.
However when i remove some elements from set changes are not propagated, and all B entities remain in
database.

Can someone help me about this, i have no idea what i am doing wrong.

Best regards.


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Fri Mar 18, 2011 6:39 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Take a look at the DELETE_ORPHAN CascadeType, it should answer your problem!
By the way, why do you have an annotaion @Column on your OneToMany mapping? Seems wrong to me ...


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Sat Mar 19, 2011 3:51 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Thank you for fast reply, that is exactly what i want. I used javax.persistance.CascadeType.ALL and
there is no DELETE_ORPHAN CascadeType at all.

Best regards.


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Sat Mar 19, 2011 5:29 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
This is directly taken from the Hibernate Doc:

Quote:
If the child object's lifespan is bounded by the lifespan of the parent object, make the parent a full lifecycle object by specifying CascadeType.ALL and org.hibernate.annotations.CascadeType.DELETE_ORPHAN (please refer to the Hibernate reference guide for the semantics of orphan delete)


But I'm assuming here that you are using Hibernate and not another implementation of JPA ...
Because not all implementations of JPA contains the DELETE_ORPHAN CascasdeType! (Actually Hibernate might even be the only one which has it ...)


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Mon Mar 21, 2011 4:34 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Hello,

I think that CascadeType.DELETE_ORPHAN is exactly what i need, but i have another problem now,
which i don't have before.

If i want to modify foreign key in class A which contains set of B objects i got following exception:
Don't change reference to a collection with cascade="all-delete-orphan".
I must say that this foreign key is not primary key of A, but surrogate key of table which just have a unique constraint.

Any help?


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Mon Mar 21, 2011 7:13 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

If you need to modify in class A the value you are using as reference to the collection of B objects then perhaps this value is just not a good choice for a FK!!


Top
 Profile  
 
 Post subject: Re: Many-To-One cascade=CascadeType.ALL not working
PostPosted: Mon Mar 21, 2011 7:42 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Hi,

But i need this option. At the beggining i used primary key of table A to be foreign key in B, but
then i realized that sometimes i need to change that value. So i choose other field to be FK.
For example:
I have Person table which can have many properties, and some unique identifier PersonNumber, which can be
modified. Then i have Account table, one person can have one or more accounts and every
account belongs to only one Person.
My idea is to set that unique indentifier PersonNumber to be FK in Account table. But Hibernate don't let me
to change it's value if i use DELETE_ORPHAN.

What other options i have?


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