You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by "Meyer, Tobias" <to...@capgemini.com> on 2013/08/06 17:59:33 UTC

Mapping of "empty" Embeddables

Hi,

I am migrating an application from Glassfish/EclipseLink to WebSphere/OpenJPA 2.2.2 and I have to following problem.
Eclipselink and OpenJPA handle "empty" Embeddables (i.e. Embeddables with all fields null) differently.
OpenJPA's default assumption is that an Embeddable is always "non-null", but EclipseLink does the opposite, meaning if all persistent members are null, the whole Embeddable is null.

The application heavily uses validation annotations on the members of the embeddables and @NotNull is one of them.
Now think of an entity that contains an embeddable which may be null. There is no problem in persisting the entity. There is also no problem when you try to load that entity from the database. But when you modify some attribute of the entity and OpenJPA tries to update it in the database, then you get a javax.validation.ConstraintViolationException because OpenJPA validated the "empty" embeddable it created when it loaded the entity from the database and there is a @NotNull annotation on a member of the embeddable which is now null.

I know this isn't a bug of OpenJPA since JPA does not specify what to expect when all members of an Embeddable are null.
But my question is: Is there a way to configure OpenJPA (globally) to behave like eclipselink in the case of "empty" embeddables, without the need to use OpenJPA specific annotations in the code like "@EmbeddedMapping(nullIndicatorColumnName="xyz")?
The java code has to be strictly JPA provider neutral, because the application (EAR) has to work on both platforms. Provider specific properties in persistence.xml or other configuration files, which don't interfere with EclipseLink would be okay.

Thanks for your help,
Tobias

_______________________________________________________________________
[Email_CBE.gif]Tobias Meyer
Custom Solution Development | Application Services

Capgemini | Hamburg
Phone: +4940254491291 - Mobile: +4915140250554
e-mail: tobias.meyer@capgemini.com
www.de.capgemini.com<http://www.de.capgemini.com/>

Luebecker Strasse 128, D-22087 Hamburg, Germany
People matter, results count.
_______________________________________________________________________


________________________________

Firma: Capgemini Deutschland GmbH
Gesch?ftsf?hrer: Dr. Michael Schulte (Sprecher) * Dr. Uwe Dumslaff * Josef Ranner
Aufsichtsratsvorsitzender: Antonio Schnieder
Amtsgericht Berlin-Charlottenburg, HRB 98814
This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.

Re: Mapping of "empty" Embeddables

Posted by Jeremy Bauer <te...@gmail.com>.
Tobias,

A feature[1] was added to OpenJPA 2.2.1 (and later) that *may* provide the
behavior you are looking for.  It allows embeddable entities to be treated
as true null references if they are marked lazy.  One difference is that
they are lazy, so if you detach the entity it will not load the embeddable
when you try to access it.  You need to use a custom annotation or
OpenJPA's ORM extensions to get this behavior.  Check out the commits
associated with the JIRA if you need an example of how to use this feature
with ORM extensions.  WebSphere allows the use of OpenJPA annotations for
application usage so unless you have a policy against using non-spec APIs,
using annotations is much simpler than creating a custom orm file.

example:

import org.apache.openjpa.persistence.Persistent;
...
    @Persistent(fetch=FetchType.LAZY, embedded=true)
    private MyEmbeddable myEmbed;

The @Persistent annotation is used instead of @Embedded.

I'm not sure if this will give you exactly what you are looking for, but I
think it is worth checking out.  I'm afraid that if you need to access the
embeddable, which will trigger a load, you could end up with the same
behavior you have now.  I don't recall the exact behavior of the feature,
though.

-Jeremy

[1] https://issues.apache.org/jira/browse/OPENJPA-2141

On Tue, Aug 6, 2013 at 10:59 AM, Meyer, Tobias
<to...@capgemini.com>wrote:

>  Hi,
>
>
>
> I am migrating an application from Glassfish/EclipseLink to
> WebSphere/OpenJPA 2.2.2 and I have to following problem.
>
> Eclipselink and OpenJPA handle “empty” Embeddables (i.e. Embeddables with
> all fields null) differently.
>
> OpenJPA’s default assumption is that an Embeddable is always “non-null”,
> but EclipseLink does the opposite, meaning if all persistent members are
> null, the whole Embeddable is null.
>
>
>
> The application heavily uses validation annotations on the members of the
> embeddables and @NotNull is one of them.
>
> Now think of an entity that contains an embeddable which may be null.
> There is no problem in persisting the entity. There is also no problem when
> you try to load that entity from the database. But when you modify some
> attribute of the entity and OpenJPA tries to update it in the database,
> then you get a javax.validation.ConstraintViolationException because
> OpenJPA validated the “empty” embeddable it created when it loaded the
> entity from the database and there is a @NotNull annotation on a member of
> the embeddable which is now null.
>
>
>
> I know this isn’t a bug of OpenJPA since JPA does not specify what to
> expect when all members of an Embeddable are null.
>
> But my question is: *Is there a way to configure OpenJPA (globally) to
> behave like eclipselink in the case of “empty” embeddables*, without the
> need to use OpenJPA specific annotations in the code like
> “@EmbeddedMapping(nullIndicatorColumnName=”xyz”)?
>
> The java code has to be strictly JPA provider neutral, because the
> application (EAR) has to work on both platforms. Provider specific
> properties in persistence.xml or other configuration files, which don’t
> interfere with EclipseLink would be okay.
>
>
>
> Thanks for your help,
>
> Tobias
>
>
>
> _______________________________________________________________________
>
> [image: Email_CBE.gif]*Tobias Meyer*
>
> Custom Solution Development | Application Services
>
>
>
> Capgemini | Hamburg
>
> Phone: +4940254491291 – Mobile: +4915140250554
>
> e-mail: tobias.meyer@capgemini.com
>
> www.de.capgemini.com
>
>
>
> Luebecker Strasse 128, D-22087 Hamburg, Germany
>
> *People matter, results count.*
>
> _______________________________________________________________________
>
>
>
> ------------------------------
>
> Firma: Capgemini Deutschland GmbH
> Geschäftsführer: Dr. Michael Schulte (Sprecher) • Dr. Uwe Dumslaff • Josef
> Ranner
> Aufsichtsratsvorsitzender: Antonio Schnieder
> Amtsgericht Berlin-Charlottenburg, HRB 98814
>  This message contains information that may be privileged or confidential
> and is the property of the Capgemini Group. It is intended only for the
> person to whom it is addressed. If you are not the intended recipient, you
> are not authorized to read, print, retain, copy, disseminate, distribute,
> or use this message or any part thereof. If you receive this message in
> error, please notify the sender immediately and delete all copies of this
> message.
>