Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Merge with Null being ignored
Merge with Null being ignored [message #872808] Wed, 16 May 2012 21:36 Go to next message
Mauro Flores is currently offline Mauro Flores
Messages: 84
Registered: September 2009
Location: Brasil
Member
When I set null to properties of an entity and call merge, the value of the column is not changed to null. If I set any other value the changing happens.

// EJB method
Foo foo = em.find(Foo.class, 1);
foo.setName(null);
Foo fooMerge = em.merge(foo);
em.flush();
return fooMerge;
//
// ....
Foo fooMerge = <call ejb method>;
System.out.println("Foo merge:" + fooMerge.getName()); // prints null
Foo fooFind = em.find(1);
System.out.println("Foo find:" + fooMerge.getName());  // prints the original value of name


This happens with an specific entity.
I can't see anything different from this entity to the others.
The field is not Transient. The merge runs inside an EJB method(CMT) running on weblogic. When I run the equivalent code standalone, the null is updated correctly.

I used to see it a lot using Openjpa implementation, but this is the first time with Eclipselink.

I appreciate some help.

Mauro.

[Updated on: Wed, 16 May 2012 21:40]

Report message to a moderator

Re: Merge with Null being ignored [message #873251 is a reply to message #872808] Thu, 17 May 2012 20:01 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1034
Registered: July 2009
Senior Member
One reason might be be that the EntityManager is different. If you read in foo prior to the EJB method call making the change, it will be managed and cached within the context with that data - completely issolated from the changes in the EJB method. That or the EJB method's transaction has not completed/commited when you perform the read in the other EntityManager context - you can't see non-commited changes. Flush is not the same as commiting the transaction.

Try setting the EclipseLink log level to finest as described here:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging
Since you are using Weblogic, to use the EclipseLink logging and log level settings instead of controlling it through the Weblogic console, you may need to specify the DefaultLogger be used.

Best Regards,
Chris
Re: Merge with Null being ignored [message #873276 is a reply to message #873251] Thu, 17 May 2012 21:24 Go to previous messageGo to next message
Mauro Flores is currently offline Mauro Flores
Messages: 84
Registered: September 2009
Location: Brasil
Member
Quote:
One reason might be be that the EntityManager is different.

No. Just one EntityManager. The same EJB (with one EM) has a method to query and other to update.

Quote:
Flush is not the same as committing the transaction.

I understand. The code of my example was not clear. The commit is done by container.

I have analysed the log, the Update SQL generated does not include the column set no null. But includes if the same field is not null.

I'll look the log more carefully and see If I can find a sign of the problem.

I thought it could have an eclipselink configuration to turn on/off this behaviour.

Thanks.


Re: Merge with Null being ignored [message #873584 is a reply to message #873276] Fri, 18 May 2012 15:34 Go to previous messageGo to next message
Mauro Flores is currently offline Mauro Flores
Messages: 84
Registered: September 2009
Location: Brasil
Member
I found out where is the problem and fix it. Although I can't explain.

Before executing the merge, there is a native query reading the same object(trying to implement an especific crazy business logic). Something like that:

// EJB method
1   Foo foo = em.find(Foo.class, 1);
2   foo.setName(null);
3   StringBuilder sql = new StringBuilder();
4   sql.append(" select t from foo t where t.pk = 1");
5   Query query = em.createNativeQuery(sql.toString(), Foo.class);
6   query.getResultList(); 
7   Foo fooMerge = em.merge(foo);
8   em.flush();
9   return fooMerge;



If the lines 3 to 6 are removed or changed by a jpql, the null is updated correctly.
If instead of null, another value is attributed to the name, the other value is updated correctly.
This native query interferes just in the null attribution.

Mauro.
Re: Merge with Null being ignored [message #873679 is a reply to message #873584] Fri, 18 May 2012 20:35 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1034
Registered: July 2009
Senior Member
Do you have any settings that would cause the native query to force a refresh on Foo or any referenced objects? If so, a lazy bidirectional relationship might get traversed for the merge operation, wiping out prior changes. The fact that a cascading refresh will still occur over lazy relationships when the relationship is eventually fetched is often overlooked when using the cascade refresh/all and lazy settings.

Unfortunately, the problem you are encountering is not going to be with what you've shown but in the settings and items that have been left out of your simple test case. I would try to pair it down to the minimum that reproduces the issue and post that if possible.

Best Regards,
Chris
Previous Topic:EclipseLink Juno Update Site
Next Topic:Problem with EclipseLink in Tomcat 7 (with RAP bridge)
Goto Forum:
  


Current Time: Sat Oct 25 15:19:36 GMT 2014

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

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