How and when to use JPA’s getReference() Method

How and when to use JPA’s getReference() Method

With the T getReference(Class<T> entityClass, Object primaryKey) and the T find(Class<T> entityClass, Object primaryKey) method, JPA’s EntityManager seems to provide 2 methods that do the same. Both of them seem to get an entity by its primary key from the database. There obviously is a difference between the 2 methods. An established and well-defined API…

Hibernate Proxies – Why they’re used and how to unproxy them

Hibernate Proxies – Why they’re used and how to unproxy them

Hibernate generates proxies to provide lazy loading for to-one associations, and you can use them to improve the performance of some of your write operations. You might have seen the class names of these proxies in your debugger or some log messages. They consist of the name of your entity class and a postfix that…

Hibernate’s @Filter Annotation – Apply Dynamic Filters at Runtime
|

Hibernate’s @Filter Annotation – Apply Dynamic Filters at Runtime

Hibernate provides 2 proprietary features that enable you to define additional filter criteria that Hibernate applies to every query that selects a specific entity class. This article will show you how to use the @FilterDef and @Filter annotations, which is the more flexible approach. You can activate and deactivate filter definitions for your current session…

Polymorphic association mappings of independent classes

Polymorphic association mappings of independent classes

JPA and Hibernate make it very easy to model associations between entities. You can model associations between 2 concrete classes or model a polymorphic association to an inheritance hierarchy. These mappings are more than sufficient for almost all of your association mappings. But sometimes, you might want to model a polymorphic association to independent entity…