mysql - 如何将此本机 SQL 查询转换为我的 java 应用程序的 jpa 查询
问题描述
SELECT b.books_name,b.books_id FROM BOOKS b JOIN AUTHOR a
ON b.AUTHOR_ID = a.AUTHOR_ID WHERE a.AUTHOR_NAME='WILLIAM';
解决方案
您需要创建 Book 和 Author 实体并建立映射。由于您的架构将 author_id 存储在 books 表中...我假设这里有多对一的映射。
@Entity
@Table(name="BOOKS")
Class Books {
@Id
Integer booksId;
String booksName;
@ManyToOne
@JoinColumn(name="AUTHOR_ID")
Author author;
... more fields
public Books(Integer booksId, String booksName) {
this.booksId = booksId;
this.booksName = booksName;
}
}
@Entity
@Table(name="AUTHOR")
Class Author {
@Id
String authorId;
String authorName;
... more fields
}
JPA 查询
@Query("Select new Books(b.booksId, b.booksName) from Books b where b.author.authorName = 'WILLIAM'")
List<Books> findMatchingBooks();
推荐阅读
- javascript - 我如何在 REACT-NATIVE 中做到这一点?[笔记本]
- python - 解析电子邮件数据中的子字符串
- python - 如何在python中处理一个非常大的文件?
- android - 如何一次将 Firebase ValueEventListner 添加到多个节点并相应地收到任何数据更改的通知
- python - PyAv av.open() 指定使用的编解码器
- javascript - 如何使我的前缀适用于小写和大写?
- javascript - 为什么我无法获得在 Vue JS 中使用 v-for 的基本示例?
- swift - 从 Firestore 获取用户配置文件的数据(swift、iOS、Xcode、firebase/firestore)
- python - 将一系列包含混合内容的CSV文件读入Python进行数据分析
- python - Scrapy Html 表到带有重复标题的 DataFrame