Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Roel De Nijs
Sheriff
Posts: 10662
144
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic