Hibernate

Hibernate / JPA Lazy Loading Pitfalls

Hibernate provides different kinds of relationships between entities like OneToMany, ManyToOne, ManyToMany or OneToOne. In the early days of Hibernate the fething strategy of all relationships was “lazy”. This stayed true for a long time and changed when Hibernate started to support JPA, because the JPA specification defines different default fetching strategies. The default fetching strategy up to Hibernate version 4.3 is: By default, Hibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications. Hibernate 4.3 manual, 20.1.1. Working with lazy associations In contrast to this the JPA specification specifies different default fetching strategies per… Read More »Hibernate / JPA Lazy Loading Pitfalls

Impact of proxy on equals and hashCode

A lot of frameworks create proxies  to apply aspect oriented programming or the lazy loading pattern. Hibernate for example uses cglib or javasissit to create lazy loading proxies. The famous springframework mainly uses java proxyies created via java.lang.reflect.Proxy.newProxyInstance(ClassLoader, Class<?>[], InvocationHandler) or cglib as well. Therefore it is important to know exactly how proxies work and the impact of proxies on your implementations. Especially when implementing basic methods like equals() and hashCode(). How proxies work A proxy is (like the name implies) not the real object. It is another object with the same interface as the real object. Since it has the same interface it can either delegate all calls to… Read More »Impact of proxy on equals and hashCode