JPA 2 Criteria Fetch Path Navigation

Agree with you about that method, and the fact that you would expect it to allow what you say. Another option would be Join<Team, Player> p = t.join(Team_.players); t.fetch(Team_.players); c.select(t).where(cb.equal(p.get(Player_.age), age)); i.e do a join(), add a fetch() for it, and then make use of the join. This is illogical and only adds to the … Read more

Multiple persistence-unit tags in one persistence.xml

JPA does not limit number of persistence units defined in persistence.xml. This warning just tells about limitation of tool in Eclipse IDE. This is told for example here http://www.eclipse.org/webtools/dali/gettingstarted.php : Currently Dali only supports one Persistence Unit and one Persistence XML file per project. Other configurations can exist in a JPA project, but the validation … Read more

SpringBoot doesn’t handle org.hibernate.exception.ConstraintViolationException

You cannot catch ConstraintViolationException.class because it’s not propagated to that layer of your code, it’s caught by the lower layers, wrapped and rethrown under another type. So that the exception that hits your web layer is not a ConstraintViolationException. In my case, it’s a TransactionSystemException. I’m using @Transactional annotations from Spring with the JpaTransactionManager. The … Read more

How to write Subqueries with In-Expressions in JPA 2.0, Criteria API?

Below is the pseudo-code for using sub-query using Criteria API. CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery(); Root<EMPLOYEE> from = criteriaQuery.from(EMPLOYEE.class); Path<Object> path = from.get(“compare_field”); // field to map with sub-query from.fetch(“name”); from.fetch(“id”); CriteriaQuery<Object> select = criteriaQuery.select(from); Subquery<PROJECT> subquery = criteriaQuery.subquery(PROJECT.class); Root fromProject = subquery.from(PROJECT.class); subquery.select(fromProject.get(“requiredColumnName”)); // field to map with main-query subquery.where(criteriaBuilder.and(criteriaBuilder.equal(“name”,name_value),criteriaBuilder.equal(“id”,id_value))); select.where(criteriaBuilder.in(path).value(subquery)); … Read more

Is Select EXISTS() possible in JPQL?

You can do a boolean query using a case expression. As of JPA 2.0 (Java EE 6) you can create a TypedQuery . String query = “select case when (count(*) > 0) then true else false end from ……” TypedQuery<Boolean> booleanQuery = entityManager.createQuery(query, Boolean.class); boolean exists = booleanQuery.getSingleResult(); In JPA 1.0 (Java EE 5) you … Read more