How to generate UUIDs as primary keys with Hibernate
| |

How to generate UUIDs as primary keys with Hibernate

Most developers prefer numerical primary keys because they are efficient to use and easy to generate. But that doesn’t mean that a primary key has to be a number. UUIDs, for example, have gained some popularity over recent years. The main advantage of a UUID is its (practical) global uniqueness which provides a huge advantage…

How to implement an AttributeConverter to support custom types
| |

How to implement an AttributeConverter to support custom types

All JPA implementations, including Hibernate, provide default mappings for a huge set of standard Java classes. You could model the attributes of all your entity classes using those mappings, and you would be able to map all columns of your table model. But this is often not the best fit for your business logic. A…

Sequence naming strategies in Hibernate 6
|

Sequence naming strategies in Hibernate 6

Hibernate 6 introduced a new configuration parameter and an interface to define the implicit naming strategy for database sequences and tables used to generate primary key values. When you migrate an existing application to Hibernate 6, you quickly recognize that change because the default naming strategy has changed. Due to that, Hibernate might try using…

Hibernate’s @NotFound Annotation – How to use it and a better alternative

Hibernate’s @NotFound Annotation – How to use it and a better alternative

Some table models don’t enforce their foreign key references by a foreign key constraint. This is a bad practice that often leads to foreign key references that point to non-existing records. When Hibernate tries to resolve such a broken reference, it throws an EntityNotFoundException. You should, therefore, define a foreign key constraint for every foreign…

How to configure List semantics in Hibernate 6
|

How to configure List semantics in Hibernate 6

Based on its javadoc, a java.util.List is supposed to represent an ordered collection of values. But that’s not necessarily the case if you use it as the type of a to-many association or an ElementCollection. As I explained before, Hibernate can handle a java.util.List as a Bag or a List. Only the List mapping persists the order…