Book Review – SQL Antipatterns

Introduction

I’ve just finished the wonderful SQL Antipatterns book by Bill Karwin. The book is a must-have reference for any developer that has to interact with a relational database system.

This post is a review of what this book is all about and why you should be interested in reading it.

Author

Bill Karwin is a distinguished software engineer, having a huge reputation on StackOverflow with gold badges for MySQL, SQL, Database or Database Design.

As I previously explained, contributing to StackOverflow is a great way to become an expert on various programming-related topics, and Bill Karwin is definitely an expert in this field.

Audience

If you are a backend developer, a team leader or a software architect, this book is definitely for you. Especially if you are a junior developer, this book is going to unravel a lot of anti-patterns which are unfortunately way too common in enterprise systems.

Content

The book covers 25 anti-patterns which are grouped into four categories:

  • Logical database design anti-patterns
  • Physical database design anti-patterns
  • Query anti-patterns
  • Application development anti-patterns

While I’ve bumped into some of these anti-patterns while developing enterprise systems, I really liked the way they were explained, and especially that we get to see a proper solution to the original problem.

Why you should buy it?

The book is very easy to read, that’s why I think it’s very relevant for junior developers as well.

Designing a database schema is not a trivial task, and it’s much cheaper to take as many good decisions as possible from the very beginning.

After reading SQL Antipatterns, you will be better prepared for designing a relational database system and the data access layer that’s needed for reading and writing to the database server.

All in all, I’m glad I read this book, and I wished I have known all these tricks back when I was a junior developer myself.

If you liked this article, you might want to subscribe to my newsletter too.

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 other three strategies (IDENTITY, SEQUENCE or TABLE) based on the underlying relational database capabilities.

While IDENTITY maps to an auto incremented column (e.g. IDENTITY in SQL Server or AUTO_INCREMENT in MySQL) and SEQUENCE is used for delegating the identifier generation to a database sequence, the TABLE generator has no direct implementation in relational databases.

This post is going to analyze why the TABLE generator is a poor choice for every enterprise application that cares for performance and scalability.

Continue reading “Why you should never use the TABLE identifier generator with JPA and Hibernate”

How to map table rows to columns using SQL PIVOT or CASE expressions

Introduction

I’m now reading the wonderful SQL Antipatterns book by Bill Karwin. The book is a great reference for any developer that needs to interact with a Relational Database System.

In this book, the Entity-Attribute-Value (EAV) model is portrayed as an Anti-Pattern. Although I must admit that a proper relational schema is almost always the best approach when designing an enterprise system, there are use cases when the EAV model is justified.

In this post, I’m going to explain when to use EAV and how to transpose a ResultSet so that rows become columns.

Continue reading “How to map table rows to columns using SQL PIVOT or CASE expressions”

The performance penalty of Class.forName when parsing JPQL and Criteria queries

Introduction

While reviewing this Hibernate Pull Request, I stumbled on the HHH-10746 Jira issue. After doing some research, I realized that this issue was reported multiple times in the past 10 years:

In this post, I’m going to explain why this issue was causing performance issues, and how it got fixed.

Continue reading “The performance penalty of Class.forName when parsing JPQL and Criteria queries”

How to customize an entity association JOIN ON clause with Hibernate @JoinFormula

Introduction

As I explained in this previous article, you can map calculated properties using Hibernate @Formula, and the value is generated at query time.

In this post, you’ll see how you can use a custom SQL fragment to customize the JOIN relationship between two entities, or an entity and a collection of embeddable types.

Continue reading “How to customize an entity association JOIN ON clause with Hibernate @JoinFormula”