简体   繁体   中英

how to create a Spring data jpa repository with a clause ON

Suppose we have two classes A and B: Class A:

public class A {private Long id;}

Class B:

public class B {private Long id; private String name ; private Boolean isDeleted = false; }

Is there a way to have results of Join query with some other conditions besides the condition ON I tried this but I doesn't work, the compiler doesn't recognize clause ON org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token :

 @Query( "SELECT new com.demo.DTO.resultsDTO(a.id) FROM A ate , B a  ON a.id = b.id WHERE b.isDeleted = false AND b.name = ?1  ")

I tried also to include the a.id = b.id in the clause WHERE, but it doesn't return results. Please help.

Your query is off, and is mixing the old-school implicit join syntax with the modern (preferred) join syntax. Here is the version you should be using:

SELECT new com.demo.DTO.resultsDTO(a.id)
FROM A a
INNER JOIN B b
    ON a.id = b.id
WHERE
    b.isDeleted = false AND
    b.name = ?1;

Your updated Java code:

@Query("SELECT new com.demo.DTO.resultsDTO(a.id) FROM A a INNER JOIN B b ON a.id = b.id WHERE b.isDeleted = false AND b.name = ?1")

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