-->
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.  [ 6 posts ] 
Author Message
 Post subject: DB entities modificarion in Interceptor methods
PostPosted: Mon May 11, 2009 1:00 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi all.

I need to modify an A entity when other (unrelated) B entity is deleted.
What I need is to load A from DB when interceptor executes 'onDelete' method over B entity.

The problem is that I can't open a new transaction because I need the Hibernate rollback management to undo my manual modification if anything goes wrong during the commit.

Also I have tried to use 'getEntity' method but it does not load the entity.

Do you understand my scenario and my problem?
Any idea?

Thanks in advance!

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: DB entities modificarion in Interceptor methods
PostPosted: Mon May 11, 2009 4:35 pm 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
Hi,
I had a similar problem where I wanted to query the DB and set a generated identifier (other than the primary key) in the interceptor. Yes, you are right that you can't start a new transaction. I think interceptor is the last possible artifact that Hibernate exposes before stuff is fired to the database. Anyway, you may however fire native SQL. This was my workaround.
https://forum.hibernate.org/viewtopic.php?f=1&t=995904
I raised the query and also answered it :)
Hope this helps you.


Top
 Profile  
 
 Post subject: Re: DB entities modificarion in Interceptor methods
PostPosted: Tue May 12, 2009 6:06 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
mazhar wrote:
Hi,
I had a similar problem where I wanted to query the DB and set a generated identifier (other than the primary key) in the interceptor. Yes, you are right that you can't start a new transaction. I think interceptor is the last possible artifact that Hibernate exposes before stuff is fired to the database. Anyway, you may however fire native SQL. This was my workaround.
https://forum.hibernate.org/viewtopic.php?f=1&t=995904
I raised the query and also answered it :)
Hope this helps you.


Thanks, it works: I have executed a SQL query within onDelete method...

But now I have the next problem:
really, A entity I want to modify is related to B in this way: A -> B, this is, there is an association (unidirectional aggregation) between both entities, so I need to set null the A's foreign key (to B) just before delete B. And I must to do this in the same transaction. But if I set a.setB(null) (with a SQL query), this is not visible in the moment of delete B so I get a ConstraintViolationException.

How could I do that?

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: DB entities modificarion in Interceptor methods
PostPosted: Tue May 12, 2009 8:47 am 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
If you are using native SQL it should work. Native SQL will bypass the first and second level cache and will be directly sent to the DB.
Can I see the query that you are using (and some related code that you feel is necessary)?


Top
 Profile  
 
 Post subject: Re: DB entities modificarion in Interceptor methods
PostPosted: Tue May 12, 2009 9:13 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
mazhar wrote:
If you are using native SQL it should work. Native SQL will bypass the first and second level cache and will be directly sent to the DB.
Can I see the query that you are using (and some related code that you feel is necessary)?


Ok, you are right... it works fine.

Thank you.

How could I evaluate as positive your answer? is there a credit system in this forum? (I can't see the link/button)

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: DB entities modificarion in Interceptor methods
PostPosted: Tue May 12, 2009 9:49 am 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
Oh I don't know about that! But I'm glad that I could be of help!
Cheers!


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