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.
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.
Based on my book, High-Performance Java Persistence, this workshop teaches you various data access performance optimizations from JDBC, to JPA, Hibernate and jOOQ for the major rational database systems (e.g. Oracle, SQL Server, MySQL and PostgreSQL).
The SQL Master Class for Java Developers training is aimed to level up your SQL skills with techniques such as Window Functions, recursive queries, Pivoting, JSON processing, and many other database querying features supported by Oracle, SQL Server, MySQL, or PostgreSQL.