persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Allow null @Embedded Objects

Open lukasj opened this issue 12 years ago • 11 comments

At the moment, it is not possible to have null @Embedded objects in JPA (while Hibernate, etc allow it). This is quite a common scenario in most domain models that JPA should take into account. The following is an example:

public class Employee {
...
@Embedded(optional=true) // This should be possible since the employee entity can be null in some
// stages of it's life-cycle
private Address address;
...
}

lukasj avatar Jan 17 '13 02:01 lukasj

  • Issue Imported From: https://github.com/javaee/jpa-spec/issues/42
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @ldemichiel

lukasj avatar Aug 31 '18 16:08 lukasj

@glassfishrobot Commented Reported by reza_rahman

lukasj avatar Jan 17 '13 02:01 lukasj

@glassfishrobot Commented neilstockton said: Just using "optional" doesn't define how a JPA provider can distinguish between a NULL embedded object and an embedded object with null fields.

The JDO spec provided a definition for this problem.

"null-indicator-column" defines which column used by the embedded object is for determining a null object, and "null-indicator-value" defines the value in that column that means we have a null object. So then the JPA provider can persist this null value in the null column on persist of a null embedded object, and ditto when retrieving.

lukasj avatar Dec 05 '16 18:12 lukasj

@glassfishrobot Commented This issue was imported from java.net JIRA JPA_SPEC-42

lukasj avatar May 05 '17 06:05 lukasj

This is an old and low priority issue. I suggest just closing this to reduce clutter (just to be clear I filed this some years ago when frankly time and resources were more abundant).

Reza Rahman Jakarta EE Ambassador, Author, Blogger, Speaker

Please note views expressed here are my own as an individual community member and do not reflect the views of my employer.

m-reza-rahman avatar Apr 27 '21 03:04 m-reza-rahman

I think this situation should be a @OneToOne rather than @Embedded.

trajano avatar Jun 23 '23 04:06 trajano

So, the interesting thing going on here is that the issue description is a bit wrong, it seems to me.

It's not the case that JPA doesn't allow an @Embedded field to be null, but rather, as far as I can tell, that the spec is actually completely silent on the topic of whether an @Embedded field can ever be null. I'm searching and I'm not finding anything.

Now, in Hibernate, we say that if all columns mapped by an @Embeddable are null, the embeddable object itself must be null. Very occasionally, someone complains, and tells me that they really have a totally legit object with all null fields and that's a totally reasonable and non-crazy thing to do, and what happens then is I look at them as if they have two heads and quickly find somewhere else to be. Frankly, I think Hibernate's behavior here is absolutely spot-on right.

But it's quite likely that other JPA implementations behave differently here, and so perhaps there's a portability issue that's worth addressing.

gavinking avatar Aug 12 '23 22:08 gavinking

How about, if the @Embeddable is final and not null which likely means it has a "storage" specified. Then it can never be null.

trajano avatar Aug 12 '23 22:08 trajano

How about, if the @Embeddable is final and not null which likely means it has a "storage" specified.

I don't understand what you mean here.

gavinking avatar Aug 12 '23 23:08 gavinking

Sorry I re-read the OP, the OP wants to have it nullable. But the scenario I am thinking of was to make it explicitly not nullable. What I was alluding to was

@Embeddable
private final SomeObject foo = new SomeObject();

would never have null for foo because the developer explicitly stated so. SomeObject can provide an empty object state by having a no-arg constructor.

trajano avatar Aug 13 '23 03:08 trajano

I see.

My knee-jerk reaction was that's a weird solution but it's growing on me.

It's an interesting idea, actually. 🤔

gavinking avatar Aug 13 '23 07:08 gavinking