Leverage the ORM.

Use relationships to reduce queries.

Relationships are directives to the ORM that describe how one model is related to another. The most common of these is the “has many” and “belongs to” relationships. An author “has many” books, and a book “belongs to” an author. Tying the tables together in this manner creates SQL joins under the covers, making it easy to get related data.

By leveraging the features of the ORM, you can avoid writing prescriptive SQL language and focus on the business issue at hand. After all, “book.author.name” seems rather intuitive and obvious in what it is getting at, without resorting to five lines of SQL.

When a new reader visits the code and sees “book.author.name”, they don’t need to spend any time parsing the code to determine the intent. It’s clearly the name of the author of the book. When you compare this to old Java code with several lines setting up a JDBC connection and concatenating strings to form a SQL query, you realize how much simpler this is.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: