Merge with Null being ignored [message #872808] |
Wed, 16 May 2012 17:36  |
Eclipse User |
|
|
|
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 17:40] by Moderator
|
|
|
|
|
|
|
Re: Merge with Null being ignored [message #1721843 is a reply to message #873584] |
Sun, 31 January 2016 19:54   |
Eclipse User |
|
|
|
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 12:37  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.24561 seconds