jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
186 stars 55 forks source link

Allow null @Embedded Objects #42

Open lukasj opened 11 years ago

lukasj commented 11 years ago

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 commented 5 years ago
lukasj commented 11 years ago

@glassfishrobot Commented Reported by reza_rahman

lukasj commented 7 years ago

@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 commented 7 years ago

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

m-reza-rahman commented 3 years ago

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.

trajano commented 1 year ago

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

gavinking commented 10 months ago

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.

trajano commented 10 months ago

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

gavinking commented 10 months ago

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.

trajano commented 10 months ago

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.

gavinking commented 10 months ago

I see.

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

It's an interesting idea, actually. 🤔