Rollback on every checked exception, whenever I say @Transactional

Custom Shortcut Annotations I know, that I could create a custom annotation, but that seems unnatural. No, this is exactly the use case for a Custom Annotation. Here’s a quote from Custom Shortcut Annotations in the Spring Reference: If you find you are repeatedly using the same attributes with @Transactional on many different methods, then … 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

JPA Criteria API – How to add JOIN clause (as general sentence as possible)

Maybe the following extract from the Chapter 23 – Using the Criteria API to Create Queries of the Java EE 6 tutorial will throw some light (actually, I suggest reading the whole Chapter 23): Querying Relationships Using Joins For queries that navigate to related entity classes, the query must define a join to the related … Read more

How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6

Specification says that derived entity should be the owning side of the relationship: 2.4.1 Primary Keys Corresponding to Derived Identities The identity of an entity may be derived from the identity of another entity (the “parent” entity) when the former entity (the “dependent” entity) is the owner of a many-to-one or one-to-one relationship to the … Read more

In JPA 2, using a CriteriaQuery, how to count results

A query of type MyEntity is going to return MyEntity. You want a query for a Long. CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); cq.select(qb.count(cq.from(MyEntity.class))); cq.where(/*your stuff*/); return entityManager.createQuery(cq).getSingleResult(); Obviously you will want to build up your expression with whatever restrictions and groupings etc you skipped in the example.

JPQL Constructor Expression – org.hibernate.hql.ast.QuerySyntaxException:Table is not mapped

according to the book “Pro EJB 3 Java Persistence API” Constructor Expressions A more powerful form of SELECT clause involving multiple expressions is the constructor expression, which specifies that the results of the query are to be stored using a user-specified object type. Consider the following query: SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e … Read more

JPA 2.0 : Exception to use javax.validation.* package in JPA 2.0

As @Korgen mentioned in comments hibernate-validator-5.x.x isn’t compatible with validation-api-1.0.x. This is because of moving to new specification JSR-303 -> JSR-349. There are two ways to solve this issue: 1. Downgrade hibernate validator version (which is implements JSR-303): <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.1.Final</version> </dependency> 2. If you don’t want to move back from hibernate validator 5 … Read more