The fullName property is calculated using the SQL Server CONCAT function, by including all names. The COALESCE function is used because CONCAT does not take a separator in SQL Server.
The columnDefinition is only useful when generating the database schema from JPA annotations (which you shouldn’t do in a production environment). Because we provided a custom @Column definition, the underlying database table looks like this:
h.id AS id1_0_0_,
h.firstName AS firstNam2_0_0_,
h.fullName AS fullName3_0_0_,
h.lastName AS lastName4_0_0_,
h.middleName1 AS middleNa5_0_0_,
h.middleName2 AS middleNa6_0_0_,
h.middleName3 AS middleNa7_0_0_,
h.middleName4 AS middleNa8_0_0_,
h.middleName5 AS middleNa9_0_0_
FROM Hero h
h.id = 1
firstName = 'Agustina',
lastName = 'de Aragón',
middleName1 = NULL(VARCHAR),
middleName2 = NULL(VARCHAR),
middleName3 = NULL(VARCHAR),
middleName4 = NULL(VARCHAR),
middleName5 = NULL(VARCHAR)
id = 1
-- After entity update action
h.fullName as fullName3_0_
FROM Hero h
WHERE h.id = 1
Again, the last SELECT query is used to ensure that the managed entity is in sync with the underlying table row.
I'm running an online workshopk on the 14th of May about The Best Way to Fetch Data with Java Persistence and Hibernate.
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
Mapping calculated entity properties is fairly easy with Hibernate, and you have multiple ways to achieve this goal.
The @Generated annotation allows you to build entity properties dynamically upon insert or update based on other property values.