Why you should use the Hibernate ResultTransformer to customize result set mappings
Introduction JPA queries allow you to fetch either entities or DTO projections. However, sometimes you want a combined result set as illustrated in this Hibernate forum question.
The best way to map a @OneToMany relationship with JPA and Hibernate
Introduction While adding a @OneToMany relationship is very easy with JPA and Hibernate, knowing the right way to map such an association so that it generates very efficient SQL statements is definitely not a trivial thing to do. In a relational database system, a one-to-many association links two tables based on a Foreign Key column so that the child table record references the Primary Key of the parent table row. As straightforward as it might be in a relational database, when it comes to JPA, the one-to-many database association can be represented… Read More
How to find which statement failed in a JDBC Batch Update
Introduction Yesterday, my Danish friend, Flemming Harms, asked my a very interesting question related to when a JDBC batch update fails. Basically, considering we are going to group several DML statements in a batch, we need a way to tell which statement is the cause of the failure. This post is going to answer this question in more detail.
The best way to soft delete with Hibernate
Introduction Each database application is unique. While most of the time, deleting a record is the best approach, there are times when the application requirements demand that database records should never be physically deleted. So who uses this technique? For instance, StackOverflow does it for all Posts (e.g. Questions and Answers). The StackOverflow Posts table has a ClosedDate column which acts as a soft delete mechanism since it hides an Answer for all users who have less than 10k reputation. If you’re using Oracle, you can take advantage of its Flashback capabilities,… Read More
How to encrypt and decrypt data with Hibernate
Introduction Today, one of my Twitter followers sent me the following StackOverflow question, and, while answering it, I realized that it definitely deserves a post of its own. In this post, I will explain how you can encrypt and decrypt data with Hibernate.
How to map the latest child of a parent entity using Hibernate JoinFormula
Introduction In this article, I’m going to explain how the Hibernate JoinFormula annotation works and how you can use it to map the latest child of a parent entity. As previously explained, the @JoinFormula is a very awesome annotation which allows you to customize the way you join entities beyond JPA @JoinColumn capabilities.
The problem of AUTO JPA GenerationType with MySQL and Hibernate
Introduction In this post, I’ll show you why you should not rely on the AUTO GenerationType strategy if you’re using MySQL and Hibernate. As I already mentioned, you should never use the TABLE identifier generator since it does not scale properly.
The JPA EntityManager createNativeQuery is a Magic Wand
Introduction I found this very interesting question on the Hibernate forum, and, in this post, I want to demonstrate to you why native SQL queries are awesome.
How to replace the TABLE identifier generator with either SEQUENCE or IDENTITY in a portable way
Introduction As previously explained, the TABLE identifier generator does not scale, so you should avoid id. However, some enterprise applications might need to run on both MySQL (which does not support database sequences), as well as Oracle, PostgreSQL, and SQL Server 2012. This is article is going to explain how easily you can achieve this goal using the JPA mapping overriding.

