Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null @Embedded Objects #42

Open
lukasj opened this issue Jan 17, 2013 · 11 comments
Open

Allow null @Embedded Objects #42

lukasj opened this issue Jan 17, 2013 · 11 comments

Comments

@lukasj
Copy link
Contributor

lukasj commented Jan 17, 2013

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
Copy link
Contributor Author

lukasj commented Jan 17, 2013

@glassfishrobot Commented
Reported by reza_rahman

@lukasj
Copy link
Contributor Author

lukasj commented Dec 5, 2016

@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
Copy link
Contributor Author

lukasj commented May 5, 2017

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

@lukasj
Copy link
Contributor Author

lukasj commented Aug 31, 2018

@m-reza-rahman
Copy link

m-reza-rahman commented Apr 27, 2021

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
Copy link

trajano commented Jun 23, 2023

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

@gavinking
Copy link
Contributor

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
Copy link

trajano commented Aug 12, 2023

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
Copy link
Contributor

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
Copy link

trajano commented Aug 13, 2023

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
Copy link
Contributor

I see.

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

It's an interesting idea, actually. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants