How do I do a “deep” fetch join in JPQL?

The JPA spec does not allow aliasing a fetch join, but some JPA providers do. EclipseLink does as of 2.4. EclipseLink also allow nested join fetch using the dot notation (i.e. “JOIN FETCH a.bs.c”), and supports a query hint “eclipselink.join-fetch” that allows nested joins (you can specify multiple hints of the same hint name). In … Read more

JPA Native Query select and cast object

You might want to try one of the following ways: Using the method createNativeQuery(sqlString, resultClass) Native queries can also be defined dynamically using the EntityManager.createNativeQuery() API. String sql = “SELECT USER.* FROM USER_ AS USER WHERE ID = ?”; Query query = em.createNativeQuery(sql, User.class); query.setParameter(1, id); User user = (User) query.getSingleResult(); Using the annotation @NamedNativeQuery … Read more

IN-clause in HQL or Java Persistence Query Language

Are you using Hibernate’s Query object, or JPA? For JPA, it should work fine: String jpql = “from A where name in (:names)”; Query q = em.createQuery(jpql); q.setParameter(“names”, l); For Hibernate’s, you’ll need to use the setParameterList: String hql = “from A where name in (:names)”; Query q = s.createQuery(hql); q.setParameterList(“names”, l);

How can I avoid the Warning “firstResult/maxResults specified with collection fetch; applying in memory!” when using Hibernate?

Although you are getting valid results, the SQL query fetches all data and it’s not as efficient as it should. So, you have two options. Fixing the issue with two SQL queries that can fetch entities in read-write mode The easiest way to fix this issue is to execute two queries: . The first query … Read more

JPQL: Receiving a Collection in a Constructor Expression

The JPA spec (Version 2.0, 2.1 and 2.2 at least) doesn’t allow the use of collections as parameters in constructor expressions. Section 4.8 defines a constructor expression like this: constructor_expression ::= NEW constructor_name ( constructor_item {, constructor_item}* ) constructor_item ::= single_valued_path_expression | scalar_expression | aggregate_expression | identification_variable A single_valued_path_expression is what it sounds like – … Read more

Doing an “IN” query with Hibernate

The syntax of your JPQL query is incorrect. Either use (with a positional parameter): List<Long> ids = Arrays.asList(380L, 382L, 386L); Query query = em.createQuery(“FROM TrackedItem item WHERE item.id IN (?1)”); query.setParameterList(1, ids) List<TrackedItem> items = query.getResultList(); Or (with a named parameter): List<Long> ids = Arrays.asList(380L, 382L, 386L); Query query = em.createQuery(“FROM TrackedItem item WHERE item.id … Read more

Spring Data JPA – Pass column name and value as parameters

The only dynamic parameter Spring JPA supports is #{#entityName}. Dynamic column names in @Query annotations are not supported., and that is what you are trying to accomplish. Your only option is to construct a query manually using either QueryDSL, Specifications or Criteria API or simply by building a query string and passing it to your … Read more