简体   繁体   English

Spring 数据 JPA 左连接两个不相关的表

[英]Spring data JPA left join on two unrelated tables

I have following two entities where there is no relationship b/w them.I would like to perform left join get data from both the tables.As spring repository provides return result on only one table how can i modify my code to get data from both the tables.我有以下两个实体,它们没有关系桌子。

@Table(value = "customer")
data class Customer(
    val id: Int,
    val name: String,
    val groupNumber: String,
)

@Table(value = "customer_policy")
data class Customer_Policy(
    val audt_id: Int,
    val policyNUmber: String,
    val groupNumber: String,
)

Here is my query这是我的查询

Select c.name,cp.policuNumber from Customer c left join customer_policy on c.groupNumber = cp.groupNumber

How Can i define an interface repository which returns the above result.如何定义返回上述结果的接口存储库。

In Spring Data JPA you have the annotation @Query where you can add your query straight in the interface methods.在 Spring 数据 JPA 中有注释@Query ,您可以在其中直接在接口方法中添加查询。

Your repository interface would look something like this:您的存储库界面将如下所示:

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository interface CustomerRepo: JpaRepository<CustomResponseEntity, Long> {

  @Query("Select c.name,cp.policuNumber from Customer c left join customer_policy cp on c.groupNumber = cp.groupNumber")
  fun findSomething():
    List<CustomResponseEntity>
}

Here you have a deeper explanation: Query annotation and examples这里有更深的解释: Query annotation and examples

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM