• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

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

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply:
 
Bartender
Posts: 653
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better late than never I suppose.
 
reply
    Bookmark Topic Watch Topic
  • New Topic