Java 如何通过JPA从连接的第二个表中检索特定行
我有两张桌子,一张是顾客台,另一张是顾客部。客户与客户部门存在一对多关系 我有一个特定的搜索条件,我需要搜索部门名称,如果它等于,我需要检索包括客户在内的所有客户部门行 这就是我试图得到的结果Java 如何通过JPA从连接的第二个表中检索特定行,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我有两张桌子,一张是顾客台,另一张是顾客部。客户与客户部门存在一对多关系 我有一个特定的搜索条件,我需要搜索部门名称,如果它等于,我需要检索包括客户在内的所有客户部门行 这就是我试图得到的结果 public interface CustomerRepository extends JpaRepository<Customer,Integer>{ @Query(value="select DISTINCT c from Customer c left join c.cust
public interface CustomerRepository extends JpaRepository<Customer,Integer>{
@Query(value="select DISTINCT c from Customer c left join c.custDept cd where cd.deptName like %?1% ")
Page<Customer> findByName(String name, Pageable pageable);
}
当我搜索DepartmentName=it时,上面的JPA查询返回以下结果
{
"content": [
{
"customerNo": 33,
"customerName": "Test1",
"industry": "High-Tech",
"country": "Australia",
"state": "Sa-Jose",
"city": "Sydney",
"custDept": [
{
"depId": 34,
"deptName": "It",
"primaryContact": "Banglore,Kormangala",
},
{
"depId": 35,
"deptName": "sales",
"primaryContact": "Banglore,Kormangala",
}
]
}
]
}
我所期待的更像是
{
"content": [
{
"customerNo": 33,
"customerName": "Test1",
"industry": "High-Tech",
"country": "Australia",
"state": "Sa-Jose",
"city": "Sydney",
"custDept": [
{
"depId": 34,
"deptName": "It",
"primaryContact": "Banglore,Kormangala",
}
]
}
]
}
如果这在JPA是不可能的,我有什么办法可以做到这一点。
谢谢你的帮助是的,我想是的。我评论说,我认为您的查询是可以的,但当结果被编组为JSON时,所有相关部门都将被检索。在编组之前,您应该查看sql输出、调试和检查查询结果,以确定是否是这种情况。我继续玩它,我或多或少是正确的 问题在于,您还没有使用查询获取custDept集,因此当为您的rest响应封送客户时,将执行一个附加查询以获取值,而附加查询只是查询所有内容 2019-05-25 14:29:35.566调试63900-[nio-8080-exec-2]org.hibernate.SQL:选择不同的customer0。customer0。customer0。no作为customer1。customer0。customer0。customer2。customer0。customer。name作为customer2,客户0.行业为行业3.客户0.左侧外部加入客户部门客户部门客户部门1.客户0.客户编号=客户部门1.客户编号客户部门1.客户编号客户部门名称在哪里?限制 2019-05-25 14:29:35.653 DEBUG 63900-[nio-8080-exec-2]org.hibernate.SQL:选择CUSTODEPT0.客户号作为客户,CUSTODEPT0.部门id作为部门id,CUSTODEPT0.部门id作为部门id,CUSTODEPT0.客户号作为客户,CUSTODEPT0.部门名称作为部门名称,custdept0\主要\作为主要\ 3\ 1\ 1\联系来自客户\部门custdept0\其中custdept0\客户\编号= 如果只需要查询提供的内容,则需要执行一次获取,以便在编组之前初始化集合的custDept。您的查询还存在其他问题。您应该使用一个sql参数:deptName,并且应该声明它,并且应该提供一个countQuery,因为您正在返回一个页面
最后请注意,根据spring文档,最好在实体中使用整数作为@Id。是的,我也这么认为。我评论说,我认为您的查询是可以的,但当结果被编组为JSON时,所有相关部门都将被检索。在编组之前,您应该查看sql输出、调试和检查查询结果,以确定是否是这种情况。我继续玩它,我或多或少是正确的 问题在于,您还没有使用查询获取custDept集,因此当为您的rest响应封送客户时,将执行一个附加查询以获取值,而附加查询只是查询所有内容 2019-05-25 14:29:35.566调试63900-[nio-8080-exec-2]org.hibernate.SQL:选择不同的customer0。customer0。customer0。no作为customer1。customer0。customer0。customer2。customer0。customer。name作为customer2,客户0.行业为行业3.客户0.左侧外部加入客户部门客户部门客户部门1.客户0.客户编号=客户部门1.客户编号客户部门1.客户编号客户部门名称在哪里?限制 2019-05-25 14:29:35.653 DEBUG 63900-[nio-8080-exec-2]org.hibernate.SQL:选择CUSTODEPT0.客户号作为客户,CUSTODEPT0.部门id作为部门id,CUSTODEPT0.部门id作为部门id,CUSTODEPT0.客户号作为客户,CUSTODEPT0.部门名称作为部门名称,custdept0\主要\作为主要\ 3\ 1\ 1\联系来自客户\部门custdept0\其中custdept0\客户\编号= 如果只需要查询提供的内容,则需要执行一次获取,以便在编组之前初始化集合的custDept。您的查询还存在其他问题。您应该使用一个sql参数:deptName,并且应该声明它,并且应该提供一个countQuery,因为您正在返回一个页面
最后请注意,根据spring文档,最好在实体中使用Integer作为@Id。我认为您的查询是可以的,但是当结果被编组为json时,所有相关的部门都将被检索。在编组之前,您应该查看sql输出并调试和检查查询结果,以确定是否是这种情况。我认为您的查询没有问题,但当结果编组为json时,将检索所有关联的部门。在编组之前,您应该查看sql输出、调试和检查查询结果,以确定是否是这种情况。
{
"content": [
{
"customerNo": 33,
"customerName": "Test1",
"industry": "High-Tech",
"country": "Australia",
"state": "Sa-Jose",
"city": "Sydney",
"custDept": [
{
"depId": 34,
"deptName": "It",
"primaryContact": "Banglore,Kormangala",
},
{
"depId": 35,
"deptName": "sales",
"primaryContact": "Banglore,Kormangala",
}
]
}
]
}
{
"content": [
{
"customerNo": 33,
"customerName": "Test1",
"industry": "High-Tech",
"country": "Australia",
"state": "Sa-Jose",
"city": "Sydney",
"custDept": [
{
"depId": 34,
"deptName": "It",
"primaryContact": "Banglore,Kormangala",
}
]
}
]
}
public interface CustomerRepository extends JpaRepository<Customer,Integer>{
@Query(value="select DISTINCT c from Customer c left join fetch c.custDept cd where cd.deptName like %:deptName% ",
countQuery = "select count ( DISTINCT c ) from Customer c left join c.custDept cd where cd.deptName like %:deptName% ")
public Page<Customer> findByName(@Param("deptName") String deptName, Pageable pageable);
{
"content": [
{
"customerNo": 1,
"custDept": [
{
"deptName": "it"
}
]
}
],