, ,

Embedded JPA Entities Under Spring Boot and Hibernate Naming

Embedded JPA Entities are nothing new to the JPA standard. By defining Embedded JPA Entities, you can define a common data type for your application. Unlike regular JPA Entities which generally follow a table per entity mapping strategy. Embedded JPA Entities are stored as additional columns in the underlying relational database table.

If you’re using Hibernate as your JPA provider under Spring Boot, and allowing Hibernate to generate the DDL for the database using the default Hibernate naming strategy provided in the default Spring Boot autoconfiguration, you may encounter exceptions when using more than Embedded JPA Entity property in a parent JPA Entity. The Spring Boot default Hibernate naming strategy does not support this. (As of Spring Boot 1.3.0)

I was coding an example for my Spring Core online course, when I ran into this problem.

Here is the exception I received when starting up the embedded Spring Boot Tomcat container.

In a nutshell, the Hibernate Mapping Exception is caused by non-unique column names for mapped to the properties of the Embedded JPA Entities.

One solution to this issue could be to use the @AttributeOverride  annotation to manually provide unique column names for my Embedded JPA Entities. But, looking at the examples in the Oracle documentation, this becomes kind of an eyesore of Annotations on my classes.

AttributeOverride Annotation Example

A More Elegant Solution to Support Multiple JPA Embedded Entities

Looking to escape annotation hell, and Google being my friend, I found a solution on Stackoverflow was to use Hibernate’s DefaultComponentSafeNamingStrategy. This will prepend the column name to the property names of the JPA Embedded Entities.

Spring Boot by default uses SpringNamingStrategy, which overrides HIbernate’s ImprovedNamingStrategy, but adds better support of foreign key naming.

To override the default Spring Boot Hibernate Naming Strategy, you just need to provide the full class name of the Hibernate Naming strategy you wish to use in your Spring Boot application.properties as follows:

application.properties

By overriding the default Spring Boot Hibernate naming strategy I was able to reduce the annotation hell of my JPA mappings.

Embedded JPA Entities Under Spring Boot Example

Here is the complete example of Embedded JPA Entities from my Spring Core Course.

Embedded JPA Entity

Address.java

JPA Entity

Customer.java

Spring Boot Configuration

  • Spring Boot 1.3.0.RELEASE
  • H2 Database (Embedded using Spring Boot defaults)
  • Hibernate 4.3.11 (via Spring Boot Starter Data JPA Maven dependency)

application.properties

Resulting Database Table

h2 database table with embedded jpa entities

Conclusion

I personally feel JPA has enough annotations to make it work. Some days it feels like we traded XML hell for annotation hell. (The more I work with JPA, the more I miss working with GORM in Grails.)

I’m glad I found this cleaner solution for supporting multiple Embedded JPA Entities without wearing out my fingers typing annotations. Hope this helps you along the way!

Share

You May Also Like

Leave a Reply