通过联接表进行JPA查询 [英] JPA Query over a join table
问题描述
我有3张桌子,例如:
A AB B
------------- ------------ ---------------
a1 a1,b1 b1
AB是A和B之间的转换表
AB is a transition table between A and B
有了这个,我的班级在这两个班级之间没有组成.但是我想知道,使用JPQL查询,是否存在AB表中A表中我的元素的任何记录.我只需要数字或布尔值即可.
With this, my classes have no composition within these two classes to each other. But I want to know that , with a JPQL Query, if any records exist for my element from A table in AB table. Just number or a boolean value is what I need.
由于AB是一个过渡表,因此没有模型对象,我想知道是否可以通过Repository对象中的@Query来做到这一点.
Because AB is a transition table, there is no model object for it and I want to know if I can do this with a @Query in my Repository object.
推荐答案
我建议使用本地查询方法界面(JPA也支持本地查询).让我们假设表A是客户,表B是产品,而AB是销售.这是获取客户订购产品列表的查询.
I suggest to use Native query method intead of JPQL (JPA supports Native query too). Let us assume table A is Customer and table B is a Product and AB is a Sale. Here is the query for getting list of products which are ordered by a customer.
entityManager.createNativeQuery("SELECT PRODUCT_ID FROM
SALE WHERE CUSTOMER_ID = 'C_123'");
这篇关于通过联接表进行JPA查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!