Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When all columns of an @Embedded field are NULL (JPA / Hibernate)

Roel De Nijs
Posts: 10763
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate sets the field in the parent object to null. Even if this field is declared final and initialized.

So if street, postalCode and city are all NULL, the home field will be set to null. That's not the behavior I'm looking for. I want an instance with all its fields set to null.

A possible workaround: handling this possible null value in the getter of this field.

But that looks very strange (certainly for a developer who doesn't know of this issue), because the field is initialized and final.

Another workaround: adding a dummy primitive variable to the @Embeddable class and annotating it with @org.hibernate.annotations.Formula, so not all columns of the @Embedded field are NULL (and thus the field in the Person class is never set to null):

Unfortunately this annotation has no JPA alternative and we prefer not to use Hibernate specific annotations, because that would lock us to a specific persistence provider

Any other workarounds or alternatives?
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!