Merge with Null being ignored [message #872808] |
Wed, 16 May 2012 21:36 |
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 #1721843 is a reply to message #873584] |
Mon, 01 February 2016 00:54 |
Ralf Heydenreich 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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.05040 seconds