|

Implement your primary key as a Record using an IdClass

There are various reasons to implement your own primary key representation. Your primary key may consist of multiple attributes. Or you’re following domain-driven design principles and want to use a custom type to add semantic meaning to it. Implementing a record and mapping it as an IdClass seems evident in these situations. A record is efficient, easy to implement, immutable…

Persist LocalDateTime, ZonedDateTime & Co with Hibernate

Persist LocalDateTime, ZonedDateTime & Co with Hibernate

The Date and Time API, introduced in Java 8, finally replaced the old java.util.Date. It’s much easier to use and finally offers separate classes representing date, date with time, and only time information. That not only improves your business code but also makes it easier to use them as entity attributes. At least if you’re…

FetchType: Lazy/Eager loading for Hibernate & JPA
|

FetchType: Lazy/Eager loading for Hibernate & JPA

Choosing the right FetchType is one of the most important decisions when defining your entity mapping. It specifies when your JPA implementation, e.g., Hibernate, fetches associated entities from the database. You can choose between EAGER and LAZY loading. The first one fetches an association immediately, and the other only when you use it. I explain…

How to generate DAOs and queries with Hibernate
|

How to generate DAOs and queries with Hibernate

Executing a query with Hibernate requires several lines of repetitive boilerplate code, and many developers have complained about that for years. You have to instantiate a Query object and set all bind parameter values before you can finally execute the query. Since version 6.3.1, Hibernate has solved this with an improved metamodel generator that provides…