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.