简体   繁体   English

如何使用 ON 子句创建 Spring 数据 jpa 存储库

[英]how to create a Spring data jpa repository with a clause ON

Suppose we have two classes A and B: Class A:假设我们有两个类 A 和 B:Class A:

public class A {private Long id;}

Class B: 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 :除了条件 ON 之外,有没有办法让 Join 查询的结果与其他条件一起出现,但我没有工作,编译器无法识别子句 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.我还尝试在 WHERE 子句中包含 a.id = b.id,但它不返回结果。 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:您更新的 Java 代码:

@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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 spring 数据 jpa 存储库创建 bean? - How to create bean for spring data jpa repository? 创建自定义存储库以使用Spring Data JPA - Create Custom Repository to Spring Data JPA 如何获取 Spring Data JPA Repository Factory? - How to get the Spring Data JPA Repository Factory? 如何检测/建议Spring Data(JPA)存储库? - How to instrument / advice a Spring Data (JPA) repository? 如何将存储库填充器与 spring 数据 -jpa 一起使用? - How to use repository populator with spring data -jpa? 如何从Spring Data JPA中的cusom存储库访问主存储库? - How to access main repository from cusom repository in Spring Data JPA? Spring JPA Data Repository无法为扩展CrudRepository的接口创建bean - Spring JPA Data Repository failed to create bean for interface that extends CrudRepository 无法创建查询元模型Spring Data JPA存储库(奇怪的行为) - Could not create query metamodel Spring Data JPA repository (Weird behaviour) Spring Data JPA如何为实现公共接口的两个域类创建单个存储库? - Spring Data JPA How to create single repository for two domain classes implementing a common interface? 如何创建未分页但已排序的 Pageable.of(unpaged, unpaged, Sort ) 到 spring 数据 jpa 存储库? - How to create unpaged but sorted Pageable.of(unpaged, unpaged, Sort ) to spring data jpa repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM