The best way to map a @OneToMany relationship with JPA and Hibernate

Introduction

In a relational database system, a one-to-many association between 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 RDBMS, when it comes to JPA, the one-to-many database association can be represented either through a @ManyToOne or a @OoneToMany association since the OOP association can be either unidirectional or bidirectional.

The @ManyToOne annotation allows you to map the Foreign Key column in the child entity mapping so that the child has an entity object reference to its parent entity. This is the most natural way of mapping a database one-to-many database association, and, usually, the most efficient alternative too.

For convenience, to take advantage of the entity state transitions and the dirty checking mechanism, many developers choose to map the child entities as a collection in the parent object, and, for this purpose, JPA offers the @OoneToMany annotation.

As I explained in my book, many times, you are better off replacing collections with a query, which is much more flexible in terms of fetching performance. However, there are times when mapping a collection is the right thing to do, and then you have two choices:

  • a unidirectional @OneToMany association
  • a bidirectional @OneToMany association

The bidirectional association requires the child entity mapping to provide a @ManyToOne annotation, which is responsible for controlling the association.

One the other hand, the unidirectional @OneToMany association is simpler since it’s just the parent-side which defines the relationship. In this article, I’m going to explain the caveats of @OneToMany associations, and how you can overcome them.

There are many ways to map the @OneToMany association. We can use a List or a Set. We can also define the @JoinColumn annotation too. So, let’s see how all these work.

Continue reading “The best way to map a @OneToMany relationship with JPA and Hibernate”

Advertisements

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.

Continue reading “How to find which statement failed in a JDBC Batch Update”

How to store date, time, and timestamps in UTC time zone with JDBC and Hibernate

Introduction

Dealing with time zones is always challenging. As a rule of thumb, it’s much easier if all date/time values are stored in the UTC format, and, if necessary, dealing with time zone conversations in the UI only.

This article is going to demonstrate how you can accomplish this task with JDBC and the awesome hibernate.jdbc.time_zone configuration property.

Continue reading “How to store date, time, and timestamps in UTC time zone with JDBC and Hibernate”

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, so you don’t need to change your application code to offer such a functionality. Another option is to use the SQL Server Temporal Table feature.

However, not all relational database systems support Flashback queries, or they allow you to recover a certain record without having to restore from a database backup. In this case, Hibernate allows you to simplify the implementation of soft deletes, and this article is gong to explain the best way to implement the logical deletion mechanism.

Continue reading “The best way to soft delete with Hibernate”

How does MVCC (Multi-Version Concurrency Control) work

Introduction

In Concurrency Control theory, there are two ways you can deal with conflicts:

  • You can avoid them, by employing a pessimistic locking mechanism (e.g. Read/Write locks, Two-Phase Locking)
  • You can allow conflicts to occur, but you need to detect them using an optimistic locking mechanism (e.g. logical clock, MVCC)

Because MVCC (Multi-Version Concurrency Control) is such a prevalent Concurrency Control technique (not only in relational database systems, in this article, I’m going to explain how it works.

Continue reading “How does MVCC (Multi-Version Concurrency Control) work”