How can I validate two or more fields in combination?

For multiple properties validation, you should use class-level constraints. From Bean Validation Sneak Peek part II: custom constraints: Class-level constraints Some of you have expressed concerns about the ability to apply a constraint spanning multiple properties, or to express constraint which depend on several properties. The classical example is address validation. Addresses have intricate rules: … 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.

JPA CascadeType.ALL does not delete orphans

If you are using it with Hibernate, you’ll have to explicitly define the annotation CascadeType.DELETE_ORPHAN, which can be used in conjunction with JPA CascadeType.ALL. If you don’t plan to use Hibernate, you’ll have to explicitly first delete the child elements and then delete the main record to avoid any orphan records. execution sequence fetch main … Read more