Hibernate inserts duplicates into a @OneToMany collection

It’s a bug in Hibernate. Surprisingly, it’s not reported yet, feel free to report it. Operations against non-initialized lazy collections are queued in order to execute them after collection is initialized, and Hibernate doesn’t handle the situation when these operations conflict with the data from the database. Usually it’s not a problem, because this queue … Read more

Programmatically loading Entity classes with JPA 2.0?

With the help of Spring I did this in a JPA compliant way. My “persistence.xml” looks empty, with no entities listed within the <persistence-unit> element. I then wrote a class that implemented PersistenceUnitPostProcessor like so: import java.util.Set; import javax.persistence.Entity; import javax.persistence.MappedSuperclass; import org.reflections.Reflections; import org.reflections.scanners.TypeAnnotationsScanner; import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo; import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor; public class ReflectionsPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor { … Read more

Spring Boot + JPA2 + Hibernate – enable second level cache

To sum everything (L2 cache and query cache) up: The first thing to do is to add cache provider (I recommend using EhCache) to your classpath. Hibernate < 5.3 Add the hibernate-ehcache dependency. This library contains EhCache 2 which is now discontinued. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>your_hibernate_version</version> </dependency> Hibernate >=5.3 In newer versions of Hibernate caches … Read more