openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Norbert Rieger" <>
Subject AW: Setting NULL value on OneToOne mapping
Date Thu, 13 Nov 2008 16:22:22 GMT
Hi mike,

thanks a lot for your detailed and quick answer, I think I understand your

So it's a provider specific handling/solution, JPA's spec does not define
how to deal with this ?

-----Urspr√ľngliche Nachricht-----
Von: Michael Dick [] 
Gesendet: Donnerstag, 13. November 2008 16:36
Betreff: Re: Setting NULL value on OneToOne mapping


When an entity is merged into the persistence context OpenJPA makes some
assumptions about whether a field was intentionally set to null or whether
it was never loaded from the database to begin with.

By default we assume that a null value indicates that the field was never
loaded. This behavior can be changed by setting the openjpa.DetachState
property. It's covered in the OpenJPA manual [1], but I'll try to give you a
quick version.

The possible values are : loaded (the default), fetch-groups, or all.

Loaded indicates that only the loaded fields of the entity will be available
after it's detached. As a result when the object is merged (or attached) any
fields that are null are ignored. The fields may never have been loaded in
the first place.

Fetch-groups indicates that fields included in the current fetch
configuration should be loaded before detaching the entity. When the entity
is merged (or attached) OpenJPA checks the current fetch group - anything
that would be included in that group that is set to null will be set on the

All indicates that all fields will be loaded before detaching the entity.
When the entity is merged (attached) all the fields are assumed to have been
loaded - if one has been set to null it will be updated in the database.

So one approach is to change the assumptions OpenJPA makes about which
fields have been loaded. Adding
<property name="openjpa.DetachState" value="fetch-groups"/>
<property name="openjpa.DetachState" value="all"/>
may resolve your problem.

Another approach is to use a detached state manager. The state manager is
the object that OpenJPA uses to track the changes made to an entity class.
By default the state manager is not serialized and saved with the entity
when it is detached. This behavior can also be configured via the
openjpa.DetachedState property:

<property name="openjpa.DetachState"

I believe this will also resolve your issue if the entity is never
serialized, at the cost of increasing the size of your detached entity. If
the entity is serialized you would want this :
<property name="openjpa.DetachState"

Sorry for the length of the response, I hope it helps to clear up your



On Thu, Nov 13, 2008 at 8:04 AM, Norbert Rieger <> wrote:

> Hi,
> I use the merge because the object is modified while detached (client <=>
> server/ejb).
> -----Urspr√ľngliche Nachricht-----
> Von: Norbert Rieger []
> Gesendet: Donnerstag, 13. November 2008 14:38
> An:
> Betreff: Setting NULL value on OneToOne mapping
> Hi,
> how do I "reset" (set foreign key field to NULL) a assocation in a
> relationship ?
> I tried to set it to "null" and then execute merge, but the UPDATE command
> does not include this.
> e.g.
> // ----------------------------
> @Entity
> class M
> {
> @Id
> long id ;
> ....
> }
> // ----------------------------
> @Entity
> class P
> {
> @Id
> long id ;
> int x;
> int y;
> @OneToOne // optional should be true by default
> M myM ;
> public void setMyM (M theM)
> { myM = theM ; }
> }
> ....
> P p = em.find (....)
> p.setX ();
> p.setY ();
> // release association
> p.setMyM ((M)null) ;
> em.merge(p) ;
> X,Y are updated, but field P.M_ID (foreign key field P=>M) stays
> How to do this, merge oder by an update command ?
> Thanks a lot in advance
> NR

View raw message