The best way to do batch processing with JPA and Hibernate
Introduction Recently, one of my followers asked me to answer a question on Quora about batch processing, and, since the question was really interesting, I decided to turn it into a blog post. In this article, you are going to find out what batch processing is, why do we use it, and how to use it properly with JPA and Hibernate.
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
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.
Why you should never use the TABLE identifier generator with JPA and Hibernate
Introduction From a data access perspective, JPA supports two major types of identifiers: assigned generated The assigned identifiers must be manually set on every given entity prior to being persisted. For this reason, assigned identifiers are suitable for natural keys. For synthetic Primary Keys, we need to use a generated entity identifier, which is supported by JPA through the use of the @GeneratedValue annotation. There are four types of generated identifier strategies which are defined by the GenerationType enumeration: AUTO IDENTITY SEQUENCE TABLE The AUTO identifier generator strategy chooses one of the… Read More
How to map calculated properties with JPA and Hibernate @Formula annotation
Introduction As I explained in this StackOverflow question, mapping calculated properties is very easy with JPA and Hibernate. In this post, I’m going to demonstrate how you can derive some entity property based on one or multiple persistent entity attributes.
The print version of High-Performance Java Persistence
One year after I published the first part of the High-Performance Java Persistence, I managed to publish the print version of the book. The book is printed on demand using Amazon CreateSpace, and it looks like as follows.
The best way to implement equals, hashCode, and toString with JPA and Hibernate
Bytecode enhancement and toString Last week, Mark Struberg, who is an Apache Software Foundation member and OpenJPA contributor, made the following statement:
How to use external XML mappings files (outside of JAR) with JPA and Hibernate
Introduction Flemming Harms has asked a very good question on Twitter: Basically, we want to move the JPA XML mappings outside of the application JAR so that we can change the mapping without affecting the jar file.
The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate
Introduction StackOverflow is a neverending source of great questions. This time, we are covering this question about using Java 1.8 Optional with JPA and Hibernate. Java 1.8 introduced the java.util.Optional container object that may or may not contain a certain value. Combining Optional and streams is very handy. Therefore, you might want that some nullable entity attributes be exposed as Optional. This article is going to demonstrate what are caveats of using Optional with entity attributes, and how you can overcome them.
The best way to lazy load entity attributes using JPA and Hibernate
Introduction When fetching an entity, all attributes are going to be loaded as well. This is because every entity attribute is implicitly marked with the @Basic annotation whose default fetch policy is FetchType.EAGER. However, the attribute fetch strategy can be set to FetchType.LAZY, in which case the entity attribute is loaded with a secondary select statement upon being accessed for the first time. This configuration alone is not sufficient because Hibernate requires bytecode instrumentation to intercept the attribute access request and issue the secondary select statement on demand.

