-->
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.  [ 2 posts ] 
Author Message
 Post subject: EntityManager.createNativeQuery and SELECT ... FOR UPDATE
PostPosted: Tue Aug 15, 2006 9:46 am 
Newbie

Joined: Mon Nov 22, 2004 10:38 am
Posts: 9
Can anyone enlighten me whether this is a valid usage of createNativeQuery?

My problem is simple. I have some JPA code that basically needs to do this:

Code:
begin transaction;
select * from A, B where A.b_id = B.id and A.id = 123 for update;
update A ...;
update B ...;
commit;


I moved this from a stored procedure to Java code by doing something like this:

Code:
BeginTransaction();
{
   A a = entityManager.createNativeQuery("select * from A where .. for update", A.class).setParameter(...).getSingleResult();
  a.setFoo(123);
  a.getB().setBar(456);
  entityManager.merge(a);
}
CommitTransaction();


This seems to work fine, but I am not sure if this is really allowed. I first tried to do this with only native queries, including for the updates, but that resulted in things being out of sync with the cache. So i ended up with this combination of a native select and 'jpa updates'.

Anyone any thoughts? Just looking for some feedback.

S.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is valid but somehow useless.
A a = entityManager.getReference(A.class, a.getId() );
entityManager.lock( a, LockModeType.READ );

Will do the same (or use the optimistic locking mechanism of a version is available).

session/lock has an even more fine grained locking scheme

_________________
Emmanuel


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