Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Just setting a property causes an sql update
Just setting a property causes an sql update [message #559187] Wed, 15 September 2010 21:06 Go to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
If I set a property of an object inside a transactional context, an sql update is generated to that property. No merge is called.!!!

Is that a correct behavior?

Can I turn off this behavior?
em.getTransaction().begin();
AlunoED ed = em.find(AlunoED.class, 4492);
ed.setNomeAlu("Foo Boo Updated");
em.getTransaction().commit();

Log
[EL Fine]: 2010-09-15 18:01:26.343--Connection(367156)--UPDATE APM_ALUNO SET NOME_ALU = ? WHERE (NRO_INT_ALU = ?)
	bind => [Foo Boo Updated, 4492]
Re: Just setting a property causes an sql update [message #559428 is a reply to message #559187] Thu, 16 September 2010 15:11 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

This is how JPA is suppose to work.

merge() in JPA is just to merge detached objects (serialized), JPA always tracks changes to all managed objects, and merge is never required for managed objects.

If you do not want something updated, then do not change it. If you want a non-managed copy of an object you can read it through another EntityManager or do not commit the transaction. EclipseLink also provides a copy() API, or you could clone it yourself, or serialize it.


James : Wiki : Book : Blog : Twitter
Re: Just setting a property causes an sql update [message #559490 is a reply to message #559428] Thu, 16 September 2010 19:30 Go to previous messageGo to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
James, thanks for explanation.

Two complementary questions:

1) The copy() melhod is available in eclipselink 1.2.1 ? I didn't find it in EntityManagerImpl

2)Instead of using one of your suggestions (copy, clone or serialization),
is a good approach to detach before making the set. Then sql update will be generated just when I call merge. The simple code below is an example of this approach that worked correctly:

    em.getTransaction().begin();
    AlunoED ed = em.find(AlunoED.class, 4492);
    org.eclipse.persistence.internal.jpa.EntityManagerImpl em2 = (org.eclipse.persistence.internal.jpa.EntityManagerImpl) em;
    em2.detach(ed);
    ed.setNomeAlu("Foo Boo Updated" + System.currentTimeMillis());    
    em.merge(ed);
    em.getTransaction().commit();
Re: Just setting a property causes an sql update [message #559954 is a reply to message #559187] Mon, 20 September 2010 14:20 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

copy() was added in 2.1.

In general if you don't want something updated it may be best just not to change it, or rollabck the transaction. Then you do not need to worry about detaching and merging.


James : Wiki : Book : Blog : Twitter
Previous Topic:ANT Task Problem
Next Topic:EclipseLink Workbench source code?
Goto Forum:
  


Current Time: Thu Apr 18 18:16:43 GMT 2024

Powered by FUDForum. Page generated in 0.02196 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top