Hibernate 4.3.6 Querysyntaxexception: Path Expected for Join

Hibernate 4.3.6 QuerySyntaxException: Path expected for join

This exception "Path expected for join" is saying:

Provide a path from one entity, to the other. The join is defined by the mapping

So we would need:

select lip.referenceId 
from Parcel as lip
// instead of this
// left join TransportFile tf
// we need a path from Parcel to TransportFile
left join lip.transportFile tf
where lip.statusDisplayName != tf.statusDisplayName'
...

The statement left join TransportFile tf is more like a SQL... which is not the case for HQL.

Our statement must be expressing navigation: left join lip.transportFile tf - i.e. join the transportFile which is related to lip.

Hibernate join to class which have property of superclass

Use a right join from C to A, and filter using the special property .class to select only items from B.

select a from C as c right join c.a as a
with a.class = 'B'

From the Hibernate Core Reference Manual:

The special property class accesses the discriminator value of an
instance in the case of polymorphic persistence. A Java class name
embedded in the where clause will be translated to its discriminator
value.

Why do I get an invalid path when I try and construct a JPQL join query?

The issue reported by Hibernate is in the first row:

final String jpqlQuery = "SELECT m FROM Message m LEFT JOIN MessageReadDate mr "...

in the LEFT JOIN statement. In hql, the JOIN must be expressing the relation, e.g.

LEFT JOIN m.MessageReadDate mr // the m is referencing the MessageReadDate

If there is no reference, we can still use that, but with Cartesian product

FROM Message m, MessageReadDate mr

in that case, the CROSS JOIN will be issued

See the:

  • 16.2. The from clause

small cite:

Multiple classes can appear, resulting in a cartesian product or "cross" join.

from Formula, Parameter
from Formula as form, Parameter as param
  • 16.3. Associations and joins

You can also assign aliases to associated entities or to elements of a collection of values using a join. For example:

from Cat as cat
inner join cat.mate as mate

How to Inner Join Two Independent Entities in Hibernate

In this case, you don't need to do a join since you limit the result with the condition d.docNumber = f.title.
Just add the condition in the where clause and use a SQL query instead of a JPQL query since it seems more matching to your need.

String sqlString= "SELECT d.docNumber " +
+ "FROM DOCUMENT d, FS_FILE f " +
+ "WHERE d.docNumber = f.title " +
+ "AND d.date > to_date('01.01.2011','dd.mm.yyyy')"

Query query = em.createNativeQuery(sqlString);
return query.getResultList();

how to resolve illegal use of LONG datatype in JPA

  • Try to use decimal or NUMERIC instead of long :

like this

CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12))
  • And don't pass a String and cast it in the Query, instead you can convert it before you pass it to the query :

For example :

String jpqlQuery = "select entity "+
"FROM Entity entity "+
"WHERE CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12)) > value1 "+
"AND CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12)) <= value2 ";

Query query = session.createQuery(jpqlQuery);

query.setParametter("value1", Long.valueOf(value1));
query.setParametter("value2", Long.valueOf(value2));

Hibernate join fetch with conditions for the right side of the join

From your comment, you should use :

select u from User u
left join fetch u.devices d
left join fetch u.ipRestrictions ir
left join fetch u.employees e
where
u.login = :login and u.enumDataState.id = :dataStateId and
(d is null or d.enumDataState.id = :dataStateId) and
(ir is null or ir.enumDataState.id = :dataStateId) and
(e is null or e.enumDataState.id = :dataStateId)

Note: the is null may not work (depends on JPA version and implementation).
You may need to use

  • u.devices is null
  • u.devices is empty

how to resolve illegal use of LONG datatype in JPA

  • Try to use decimal or NUMERIC instead of long :

like this

CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12))
  • And don't pass a String and cast it in the Query, instead you can convert it before you pass it to the query :

For example :

String jpqlQuery = "select entity "+
"FROM Entity entity "+
"WHERE CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12)) > value1 "+
"AND CAST(SUBSTRING(entity.nameId, 2, 12) AS DECIMAL(2,12)) <= value2 ";

Query query = session.createQuery(jpqlQuery);

query.setParametter("value1", Long.valueOf(value1));
query.setParametter("value2", Long.valueOf(value2));


Related Topics



Leave a reply



Submit