简体   繁体   中英

JPQL query - hibernate: with-clause not allowed on fetched associations

I have JPA Repository and JPQL query like this:

@Query("SELECT c from Campaign c" +
        " left join fetch c.postsList p on p.status = :postStatus" +
        " left join fetch p.platform" +
        " left join fetch c.campaignStatistics stat on stat.updateDate = :updateDate" +
        " where c.id =:id")
Campaign findCampaignWithPosts(
        @Param("id") Long id,
        @Param("postStatus") PostStatus postStatus,
        @Param("updateDate") LocalDate updateDate);

But it doesn' work. I get:

org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause not allowed on fetched associations; use filters

I went to study information about the JPA 2.1 specification and that's what I found:

The join condition used for a join comes from the mapping's join columns. This means that the JPQL user is normally free from having to know how every relationship is joined. In some cases it is desirable to append additional conditions to the join condition, normally in the case of outer joins. This can be done through the ON clause. The ON clause is defined in the JPA 2.1 specifiation, and may be supported by some JPA providers. EclipseLink : Hibernate : TopLink - support the ON clause.

It should be noted that this type of query does not help me at all, becouse in this query sometimes i get null, where clause use after join tables.

   @Query("SELECT c from Campaign c" +
            " left join fetch c.postsList p" +
            " left join fetch p.platform" +
            " left join fetch c.campaignStatistics stat" +
            " where c.id =:id" +
            " and p.status = :postStatus" +
            " and stat.updateDate = :updateDate")

What to do in this case? Is there no alternative solution other than how to use the native query? But then the meaning of almost all JPQL queries is lost. I use hibernate version 5.2.12

You are right, when you add and p.status = :postStatus it filters out the results where the right side of the join does not exist (ie when the Campaign has no posts).

What worked for me is to add an OR clause to accept the case where the right side of the join is NULL.

So, you should replace and p.status = :postStatus with and (p IS NULL OR p.status = :postStatus) .

So this request should work :

@Query("SELECT c from Campaign c" +
            " left join fetch c.postsList p" +
            " left join fetch p.platform" +
            " left join fetch c.campaignStatistics stat" +
            " where c.id =:id" +
            " and (p is NULL OR p.status = :postStatus)" +
            " and stat.updateDate = :updateDate")

As for the error message you received, I think you should not add the ON clause because that is already handled by JPA/Hibernate.

I am using Hibernate 5.0.12.

There is no alternative solution, because when you have a predicate like this:

and (p is NULL OR p.status = :postStatus)

then 3 cases appear:

  1. getPostsList() returns empty list
  2. getPostsList() returns list with at least one element with queried status
  3. getPostsList() returns list with no element with queried status

In state from 3 case you will lose all rows with Campaign completely. But fetch ones with 1 state

Because left join will not join nulls, when list of postList is not empty .

It's not a good idea use WHERE condition on fetch alias

Try this

@Query("SELECT c from Campaign c" +
        " left join c.postsList p" +
        " left join fetch c.postsList " +
        " left join fetch p.platform " +
        " left join c.campaignStatistics stat" +
        " left join fetch c.campaignStatistics " +
        " where c.id =:id" +
        " and (p is NULL OR p.status = :postStatus)" +
        " and stat.updateDate = :updateDate")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM