Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Is it possible to do batch updates using EclipseLink/JPA?

Interesting, yes there is no equivalent to registerExistingObject() in JPA. 
You will need to get the UnitOfWork from the EntityManager and call the API.

In general I would strongly recommend you read using JPA, not JDBC, then the
object will be in the cache.

JPQL Update queries should also get batched.


shkolnik wrote:
> 
> Thank you for reply. 
> 
> Cache will not help, because I always get detached objects that were
> loaded using plain JDBC. However I can guarantee that these objects exist
> in database so I don't want JPA to check for their existance. So in
> EclipseLink I would use unitOfWork.registerExistingObject(person) which
> will return to me a clone that I can modify and at the time of commit, it
> will know which attributes were modifed. So my question is: can this
> functionality be implemented using only JPA interfaces or am I forced to
> use EclipseLink? I thought that perhaps @ExistenceChecking(value =
> ExistenceType.ASSUME_EXISTENCE) will do the trick, but it doesn't seem to
> work.
> 
> Initially I thought I would be able to accomplish this requirement by
> using a named query:
> "update person p set p.age = :age where p.id = :id". And it works for
> executing individual updates, but I don't thinks it's possible to execute
> it is as a batch update. It seems there is no addBatch() equivalent in
> Query interface.
> 
> Any additional suggetions will be appreciated.
> 
> 
> 
> 
> 
> James Sutherland wrote:
>> 
>> You can use batch writing using the persistence.xml property,
>> 
>> "eclipselink.jdbc.batch-writing"="JDBC"
>> 
>> See,
>> http://www.eclipse.org/eclipselink/api/1.1/org/eclipse/persistence/config/PersistenceUnitProperties.html#BATCH_WRITING
>> 
>> The merge() API must read the object from the database if it does not
>> exist in the cache.  If you do not want it to read from the database,
>> ensure you have caching enabled (it is by default), or perhaps increase
>> you cache size or type.  You must read an object before updating it, as
>> otherwise you do not know what changed, or what related objects changed.
>> 
>> EclipseLink will always compute what fields changed, whether using
>> weaving or not.  Weaving just allows the changes to be tracked through
>> events, instead of through backup copies.
>> 
>> 
>> 
>> shkolnik wrote:
>>> 
>>> I have a very simple requirement: update a large number of records in
>>> DB. 
>>> Using plain JDBC it would look like this: 
>>> PreparedStatement stmt = connection.prepareStatement("update person set
>>> age = ? where id = ?"); 
>>> String age = getAge(); 
>>> for(Person p : people) { 
>>>      stmt.setString(1, age); 
>>>      stmt.setLong(2, p.getId()); 
>>>      stmt.addBatch(); 
>>> } 
>>> stmt.executeUpdate(); 
>>> 
>>> Using EclipseLink, I get the same effect like this: 
>>> 
>>> for(Person p : people) { 
>>>      p = unitOfWork.registerExistingObject(p); 
>>>      p.setAge(age); 
>>> } 
>>> unitOfWork.commit(); 
>>> 
>>> My question: is it possible to implement this using only JPA interfaces?
>>> I tried using EntityManager.merge(), but it always executes a select
>>> statement to check if record exists in db. I tried adding
>>> @ExistenceChecking(value = ExistenceType.ASSUME_EXISTENCE) to the
>>> entity, but it doesn't help. The second part of the question is even if
>>> its possible to somehow force JPA not to check database, is it possible
>>> to implement change tracking without any kind of weaving or will it
>>> update all the fields? Maybe it's possible to specify which fields I
>>> want to be updated? 
>>> 
>>> Thank you for your help.
>>> 
>> 
>> 
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Is-it-possible-to-do-batch-updates-using-EclipseLink-JPA--tp22833966p22849661.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top