The best way to do batch processing with JPA and Hibernate

Introduction

Recently, on 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.

Continue reading “The best way to do batch processing with JPA and Hibernate”

Advertisements

How does a JPA Proxy work and how to unproxy it with Hibernate

Introduction

The JPA lazy loading mechanism can either be implemented using Proxies or Bytecode Enhancement so that calls to lazy associations can be intercepted and relationships initialized prior to returning the result back to the caller.

Initially, in JPA 1.0, it was assumed that Proxies should not be a mandatory requirement, and that’s why @ManyToOne and @OneToOne associations use an EAGER loading strategy by default. However, EAGER fetching is bad for performance so it’s better to use the FetchType.LAZY fetching strategy for all association types.

In this article, we are going to see how the proxy mechanism works and how you can unproxy a given Proxy to the actual entity.

Continue reading “How does a JPA Proxy work and how to unproxy it with Hibernate”

How to get started with CockroachDB

Introduction

CockroachDB is a really interesting database system, getting the best of both RDBMS and NoSQL. It’s been developed by former Google developers, and it’s inspired by Google Spanner. However, unlike Google Spanner, which is offered as a service in Google Cloud, CockroachDB is an open-source database that can be installed on premise.

Also, CockroackDB allows you to use the PostgreSQL drivers as opposed to Spanner which only supports the gRPC protocol. So, you can practically reuse all the frameworks that have emerged in the Java ecosystem like connection pools, monitoring proxies (e.g. FlexyPool) or data access frameworks (e.g. Hibernate).

Unlike a typical RDBMS, CockroachDB is designed to be globally distributed and strongly resilient to disasters, hence its very unusual name. But what’s really exciting about CockroachDB is its non-locking timestamp ordering concurrency control mechanism which allows CockroachDB to run in Serializable isolation level, even if the database is massively distributed. Intriguing, right?

Continue reading “How to get started with CockroachDB”

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 @OneToMany 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 @OneToMany 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”