-->
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: Change update query for merge() in JPA
PostPosted: Tue Dec 25, 2007 3:57 pm 
Newbie

Joined: Mon Oct 15, 2007 11:44 am
Posts: 7
Hello,
I'm starting with JPA, so please help with basic JPA stuff !
In JPA, to update an entity (say customer) we use:
Code:
em.merge(customer);

1. What if I want to update ONLY certain fields ? (essentially i'm trying to change underlying UPDATE query for merge() method)

2. I have a field 'rowversion' which should be incremented when merge() called. How to do that ?
SQL query would have been:
Code:
UPDATE customer_table SET customer_city='city', rowversion=rowversion+1


Thank You.


Top
 Profile  
 
 Post subject: Update fields
PostPosted: Fri Dec 28, 2007 8:27 am 
Newbie

Joined: Wed Dec 26, 2007 9:36 am
Posts: 7
You have two options here (there might be some better way, I am pretty new to JPA/ORMs myself):

The first option has two steps (code is just to give you an idea):
Code:
// retreive the existing customer from db
Customer dbCustomer = em.findById...(customer.getId());
// update attributes you want to, and leave the rest
dbCustomer.setPhoneNr(customer.getPhoneNr);
dbCustomer.setRowVersion(dbCustomer.getRowVersion() + 1);
// save the updated customer
em.merge(dbCustomer);



The second option will involve running a sql/hsql query, pretty much on the same line as the query in your post.


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.