如何编写JPA查询
学习如何编写 JPA 查询。请告诉我是否可以更有效地编写以下查询,可以在单个选择语句中。可能是加入,但不知道如何去做。
class Relationship {
@ManyToOne
public String relationshipType; //can be MANAGER, CUSTOMER etc
@ManyToOne
public Party partyFrom; // a person who has a relation
@ManyToOne
public Party partyTo; // a group a person relate to
}
查询:
String sql = "";
sql = "select rel.partyTo";
sql += " from Relationship rel";
sql += " where rel.partyFrom = :partyFrom";
sql += " and rel.relationshipType= :typeName";
Query query = Organization.em().createQuery(sql);
query.setParameter("partyFrom", mgr1);
query.setParameter("typeName", "MANAGER");
List<Party> orgList = query.getResultList();
String sql2 = "";
sql2 = "select rel.partyFrom";
sql2 += " from Relationship rel";
sql2 += " where rel.partyTo = :partyToList";
sql2 += " and rel.relationshipType = :typeName2";
Query query2 = Organization.em().createQuery(sql2);
query2.setParameter("partyToList", orgList);
query2.setParameter("typeName2", "CUSTOMER");
List<Party> personList2 = query2.getResultList();
这两个查询都有效。查询 1 返回组列表,其中人员 (mgr1) 与其具有关系 MANAGER。查询 2 返回他们属于查询 1 返回的组的所有客户。实际上,我得到了他们所属的人员(客户)的列表,该人员与人员 (mgr1) 具有关系为 MANAGER。
是否可以将它们组合成单个 sql 语句,这样可能只有一个数据库访问?
Learning how to write JPA query. Please advise me whether it possible to write the below queries more efficiently, may be in a single select statement. May be a join, but not sure how to do it.
class Relationship {
@ManyToOne
public String relationshipType; //can be MANAGER, CUSTOMER etc
@ManyToOne
public Party partyFrom; // a person who has a relation
@ManyToOne
public Party partyTo; // a group a person relate to
}
Queries:
String sql = "";
sql = "select rel.partyTo";
sql += " from Relationship rel";
sql += " where rel.partyFrom = :partyFrom";
sql += " and rel.relationshipType= :typeName";
Query query = Organization.em().createQuery(sql);
query.setParameter("partyFrom", mgr1);
query.setParameter("typeName", "MANAGER");
List<Party> orgList = query.getResultList();
String sql2 = "";
sql2 = "select rel.partyFrom";
sql2 += " from Relationship rel";
sql2 += " where rel.partyTo = :partyToList";
sql2 += " and rel.relationshipType = :typeName2";
Query query2 = Organization.em().createQuery(sql2);
query2.setParameter("partyToList", orgList);
query2.setParameter("typeName2", "CUSTOMER");
List<Party> personList2 = query2.getResultList();
Both the queries work. Query 1 returns a list of groups, where the person (mgr1) has a relation MANAGER with. Query 2 returns all the Persons they are CUSTOMER to the groups returned by query 1. In effect, I get a list of Person they are belong to (customer) the same group where the Person (mgr1) has a relation MANAGER with.
Is it possible to combine them into single sql statement so possibly only one db access?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将一个查询嵌套在另一个查询中,并使用“where in”子句指定外部查询应从内部查询中获取客户。
您的调用代码像以前一样传递
typeName
、typeName2
和partyFrom
参数。不需要PartyTo
参数,因为数据来自子选择(内部查询)。您可以使用自联接实现相同的效果,并使用 where 子句过滤左侧的经理和客户在右侧,但使用“in”子句在语义上更清晰。
编辑:我将 .id 添加到子选择中,我认为这是需要的。
You literally nest one query inside the other, and use a "where in" clause to specify that the outer query should fetch customers from the inner query.
Your calling code passes
typeName
,typeName2
, andpartyFrom
parameters as before.PartyTo
parameter is not needed, since the data comes from the subselect (inner query.)You can achieve the same thing using a self join, with a where clause that filters managers on the left side, and customers on the right side, but using an 'in' clause is semantically clearer.
EDIT: I addded .id to the subselect, which I think is needed.
这不是问题的答案,而是帮助其他人,以防有人使用 JPQL 在 Spring Data JPA 中查找 @OneToMany 关系,因为这个问题与 JPA 相关,所以想分享我的 2 美分,提前道歉
}
表中的一些数据就像
从 Vehicle_Series 选择 *
从 Body_Type 选择 *
This is not answer to question but helping other folks in case if someone looking into @OneToMany relation in Spring Data JPA using JPQL, because the question is related to JPA so thought to share my 2-cents, apologize in advance
}
Some data in tables like
Select * from Vehicle_Series
Select * from Body_Type