Leanpub Header

Skip to main content

Opinionated JPA with Querydsl

We use JPA/ORM because it's convenient and available. How hard can it be? Later our to-one relationships don't want to load lazily and the performance suffers. This book fills non-reference niche, using JPA differently and exploring options how to get around some of the limitations. And it shows Querydsl for better query code in the process.

Free With Membership

With Membership

Free!

$8.00

You pay

$8.00

Author earns

$6.40
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
350
Readers
234
Pages
About

About

About the Book

ORM is a neat idea, typically used together with rich domain model. JPA, currently in version 2.2, is a Java specification that is a common ground for most Java ORMs. But ORM is also a love-hate topic, with stickers like "Vietnam of computer science", often called an antipattern.

In this book we'll talk about many interesting JPA aspects and put them into the context of other persistence options. I'll also try to explore how to use JPA for queries that are more SQL friendly, but still benefiting from convenient features not available with plain old JDBC. We will discuss non-JPA feature called entity (or ad-hoc) joins - supported by both EclipseLink and Hibernate. We'll also cover some typical problems (lazy loading and related N+1 select).

Querydsl will be with us along the way, because compared to Criteria API it allows us to write much cleaner queries in a fluent API fashion.

Share this book

Categories

Author

About the Author

Richard "Virgo" Richter

Software developer since forever, professionally since 2000. Very familiar with Java ecosystem, but open to learning new languages and technologies as I don’t believe in narrow specialization. I like the idea of an agile and learning environment (like Joy, Inc.). I also believe in testing automation, continuous delivery, clean code and also other, often rather peopleware-ish topics. I'm exploring functional programming and domain-driven design, among others.

Contents

Table of Contents

Preface

  1. Is this book for you?
  2. Not in the book
  3. Other JPA books
  4. Books on performance of the database and persistence layer
  5. Looking back to Patterns of Enterprise Application Architecture
  6. Structure of the book
  7. Using code examples
  8. My writing background
  9. Feedback
  10. IJPA Good Times, Bad Times

1.Good Parts

  1. JPA is standard
  2. Database independence
  3. Natural SQL-Java type mapping
  4. Flexible mapping
  5. Unit of work
  6. Declarative transactions
  7. Other JPA 2.1 goodies

2.The Missing Parts

  1. Compared to ORM providers
  2. Comparing JPQL to SQL
  3. Other missing parts

3.Questionable parts

  1. Lazy on basic and to-one fields
  2. Generated SQL updates
  3. Unit of work vs queries
  4. We can’t escape SQL and relational model
  5. Additional layer
  6. Big unit of work
  7. Other entity manager gotchas
  8. JPA alternatives?

4.Caching considerations

  1. Caching control
  2. Second-level cache vs queries
  3. Explicit application-level cache
  4. Conclusion

5.Love and hate for ORM

  1. Vietnam of Computer Science
  2. Not much love for ORM
  3. But a lot of hate
  4. Is tuning-down a way out?
  5. And there is more
  6. IIOpinionated JPA

6.Tuning ORM down

  1. Price for relations
  2. How does this affect my domain model?
  3. When to tune down and when not?

7.No further step without Querydsl

  1. Simple example with Querydsl
  2. Comparison with Criteria API
  3. Comparison with JPQL
  4. What about the cons?
  5. Be explicit with aliases
  6. Functions and operations
  7. Subqueries
  8. Pagination
  9. Tuple results
  10. Fetching to-many eagerly
  11. Querydsl and code style
  12. More

8.Troubles with to-one relationships

  1. Simple example of @ManyToOne
  2. How about going lazy?
  3. Dual relationship mapping
  4. Using projections for fetching
  5. Entity update scenario
  6. Alternative entity “views”
  7. Summary

9.Removing to-one altogether

  1. Why do we need ON anyway?
  2. Dropping to-one annotations
  3. Ad hoc joins across tables
  4. Loading the relationship from the entity
  5. Loosing information without mapping annotations
  6. Wrapping it up

10.Modularity, if you must26

  1. Everybody knows layers
  2. Components based on features
  3. Multiple JARs with JPA entities
  4. Distributing persistence.xml – do we need it?
  5. It doesn’t start anyway
  6. Shared cache mode
  7. Putting the Spring configuration together
  8. Common base entity classes in separate JAR
  9. Cutting cyclic dependencies
  10. Conclusion
  11. IIICommon problems

11.Avoid N+1 select

  1. Anatomy of N+1 select
  2. Eager to-one without joins
  3. Eager to-many relationships
  4. Lazy relationships triggered later
  5. Paginating with to-many
  6. Wrapping up N+1 problem

12.Mapping Java enum

  1. Naive approach
  2. Encapsulated conversion
  3. Conversion micro-framework
  4. Refinement?
  5. Simplified static resolving
  6. Alternative conversions within entities
  7. Java 8 and reverse enum resolution
  8. Relational concerns

13.Advanced Querydsl

  1. Introducing handy utility classes
  2. Tricky predicate parts
  3. Groups of AND/OR predicates
  4. Operator precedence and expression serialization
  5. Querydsl Expression hierarchy
  6. Constant expressions
  7. Using FUNCTION template
  8. Update/delete clauses and exception handling
  9. JPAQueryFactory
  10. Detached queries
  11. Working with dates and java.time API
  12. Result transformation
  13. Note about Querydsl 3 versus 4
  14. IVFinal thoughts
  15. JPA/ORM – yes or no?
  16. JPA or concrete ORM provider?
  17. JPA, Querydsl and beyond
  18. This book
  19. Appendices

H2 Database

  1. Starting it
  2. Using it as a devel/test DB

Project example

  1. Querydsl and Maven
  2. Querydsl and Gradle

Bugs discovered while writing this book

List of acronyms

  1. Resources
  2. Books
  3. Online articles, blogs
  4. Other online resources

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub