Skip to main content


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 FloresFriend
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 DelahuntFriend
Messages: 1389
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 FloresFriend
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 FloresFriend
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 messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
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
Re: Merge with Null being ignored [message #1721843 is a reply to message #873584] Mon, 01 February 2016 00:54 Go to previous messageGo to next message
Ralf Heydenreich is currently offline Ralf HeydenreichFriend
Messages: 235
Registered: July 2009
Senior Member
Am 18.05.2012 um 17:34 schrieb Mauro Flores:
> 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.


Hi all,
although this thread is very old I have exactly the same problem, but
inside a Java SE application (without EJB). I'm using Eclipselink inside
an RCP application.
Now, if I want to "cut" a reference from an object by setting the
object's attribute value to null it is ignored. If I set other
attributes (Strings) to any other value (on the same object!) the
changes get persisted in the database. Is there a switch to store also
null values by EclipseLink or is it not possible by design?

Here's my update method:

public T update(T object) throws StoringException {
try {
checkConnection();
EntityTransaction trx = getEntityManager().getTransaction();
trx.begin();
object = getEntityManager().merge(object);
trx.commit();
} catch (SQLException e) {
throw new StoringException("Error updating to the database.", e, object);
}
return object;
}


Regards,
Ralf.
Re: Merge with Null being ignored [message #1721932 is a reply to message #1721843] Mon, 01 February 2016 17:37 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This isn't quite the same problem - the thread is about an issue that seems to be reseting values in unexpected spots. I'd recommend starting a new thread and turn on logging to see what SQL is generated. Post a bit more - what is in your object you are merging, how it was read in, etc. For debugging purposes, try calling find before the merge and see what is in the entity read in. If the values are already null, change won't be detected - if this is the case, check that you aren't using 'read-only' options on your queries and then making changes.
Previous Topic:ORA-24816 on insert with LOB
Next Topic:Using a REST Service as Datasource
Goto Forum:
  


Current Time: Fri Apr 26 18:44:18 GMT 2024

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

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

Back to the top