-->
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.  [ 5 posts ] 
Author Message
 Post subject: DELETE_ORPHAN on Web
PostPosted: Thu Dec 06, 2007 5:18 pm 
Newbie

Joined: Thu Dec 06, 2007 5:04 pm
Posts: 4
Hi there

Actually I'm getting a very strange problem

I'm using JPA with Hibernate and DELETE_ORPHAN on @OneToMany relationships. Who makes my EntityManager and manages my transaction is Spring Framework.

I made some Unit/Integration tests and when I run they, everything works fine, exactly as I expected, even I use EXACLTY the configuration on web

but, when I use it on web, DELETE_ORPHAN doesn't work and I don't know what is happening. And other problem is, let's suppose this piece of code
Code:
public void removeElement( Element element ) {
  element.setParent( null );
  this.getElements().remove( element );
}

public void addElement( Element element ) {
  element.setParent( this );
  this.getElements().add( element );
}


As you can see, it's a common way to add/remove element on a relationship and, when I use merge() operation on EntityManager, my EntityManager must cascade the operations, right?

but I saw one crazy thing

if I execute this line
Code:
this.getElements().remove( element );

doesn't let my "element" object to be updated. In other words, if I change other property in "element" object, it won't update this property.
BUT, if I remove that line the "element" object will be updated

anybody got a similar situation like that?


Top
 Profile  
 
 Post subject: Re: DELETE_ORPHAN on Web
PostPosted: Thu Dec 06, 2007 7:57 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Leozin wrote:
Code:
this.getElements().remove( element );

doesn't let my "element" object to be updated. In other words, if I change other property in "element" object, it won't update this property.
BUT, if I remove that line the "element" object will be updated

anybody got a similar situation like that?


I am not sure if I understand the problem correctly but if the only way an element is updated is through its parent then you can't expect the element to be updated if it is removed from its parent list. Therefore the behaviour you are seeing is the right behaviour. However, the element should be removed after the session flushes. Is that what is happening?


Farzad-


Top
 Profile  
 
 Post subject: Re: DELETE_ORPHAN on Web
PostPosted: Fri Dec 07, 2007 9:00 am 
Newbie

Joined: Thu Dec 06, 2007 5:04 pm
Posts: 4
farzad wrote:
Leozin wrote:
Code:
this.getElements().remove( element );

doesn't let my "element" object to be updated. In other words, if I change other property in "element" object, it won't update this property.
BUT, if I remove that line the "element" object will be updated

anybody got a similar situation like that?


I am not sure if I understand the problem correctly but if the only way an element is updated is through its parent then you can't expect the element to be updated if it is removed from its parent list. Therefore the behaviour you are seeing is the right behaviour. However, the element should be removed after the session flushes. Is that what is happening?


Farzad-


Exaclty, but the problem is: In the web environment my element isn't removed from database, but if I test in my TestNG tests, I do the same web configuration (with Spring, AOP Transactions and bi-injection).

In other words, like I did

Code:
this.getElements().remove( element );


and, after that, I do

Code:
em.merge( parent );


DELETE_ORPHAN doesn't work, but ONLY in web environment :(

I did a lot of tests, but I did a work around to work on web, the same work-around that I did once, when I used to use Toplink JPA instead of Hibernate EntityManager. I'm using Hibernate with JPA (now) because Hibernate is better, there are some JPA extensions (like DELETE_ORPHAN and Criteria) that don't have on Toplink


Top
 Profile  
 
 Post subject: Re: DELETE_ORPHAN on Web
PostPosted: Fri Dec 07, 2007 12:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Leozin wrote:
I did a lot of tests, but I did a work around to work on web, the same work-around that I did once, when I used to use Toplink JPA instead of Hibernate EntityManager. I'm using Hibernate with JPA (now) because Hibernate is better, there are some JPA extensions (like DELETE_ORPHAN and Criteria) that don't have on Toplink



Umm, there are two things I can think of. First, DELETE_ORPHAN does not work on unmanaged collections. This can be verified with the document. Second, the problem is somewhere in your transaction management. I mean probably your session does not get flushed to database. Well, to make things more clear, why dont you send us what you do in your tests and what you do in your web app. Maybe the comparison will point us to the problem.


Top
 Profile  
 
 Post subject: Re: DELETE_ORPHAN on Web
PostPosted: Fri Dec 07, 2007 1:58 pm 
Newbie

Joined: Thu Dec 06, 2007 5:04 pm
Posts: 4
farzad wrote:
Umm, there are two things I can think of. First, DELETE_ORPHAN does not work on unmanaged collections. This can be verified with the document. Second, the problem is somewhere in your transaction management. I mean probably your session does not get flushed to database. Well, to make things more clear, why dont you send us what you do in your tests and what you do in your web app. Maybe the comparison will point us to the problem.


This is very strange, because I have EXACTLY the same kind of transaction management in my TestNG Tests

I thought too about my unmanaged collections. But how can I manage they? In other words, maybe I have to do something special in web environment to transform my collections in managed

Ah, I forgot: My collection is a map collection with an enumeration as key


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