How to join unassociated entities with JPA and Hibernate
| |

How to join unassociated entities with JPA and Hibernate

Representing associations between entities as attributes is one of the most comfortable and popular features of JPA and Hibernate. It makes it very easy to navigate from one entity to one or more associated entities in your Java code, like from a Person entity to the corresponding Address entity. You can also use the association…

How to define named queries at runtime with JPA 2.1
| |

How to define named queries at runtime with JPA 2.1

Defining a static named query via the @NamedQuery annotation is the most common way in JPA. It has the main advantage that the persistence provider can compile and validate the query at start-up time. But you also have to define the query statically at compile time. OK, you can still define a dynamic query at runtime but how do you…

5 ways to initialize lazy associations and when to use them
| |

5 ways to initialize lazy associations and when to use them

Lazy loading of associations between entities is a well established best practice in JPA. Its main goal is to retrieve only the requested entities from the database and load the related entities only if needed. That is a great approach if you only need the requested entities. But it creates additional work and can be…

JPA Entity Graphs: How to Dynamically Define and Use an EntityGraph
| |

JPA Entity Graphs: How to Dynamically Define and Use an EntityGraph

This is my second post on Entity Graphs. The first post described the usage of named entity graphs. These can be used to define a graph of entities and/or attributes at compile time that shall be fetched with a find or query method. Dynamic entity graphs do to the same but in a dynamic way. This…