我有一個業務需求來撰寫一個從給定代理代碼回傳子代理代碼串列的服務。此agent_details表如下。

這個表agent_code是唯一的,但是reporter_code可以重復。每個經紀人都有一個記者。此外,記者是代理人。根據該表,代理人和記者的聯系如下。

我需要一項服務來通過提供代理代碼來獲取子代理串列。作為給出1011應該回傳的例子[1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010]
給予時1005應該回傳[ 1002, 1003]。
我正在為這個專案使用 Jpa 存盤庫。我的物體類和存盤庫類如下。如何解決這個問題 sql 查詢或 jpa 存盤庫?
@Data
@Entity
@Table(name = "agent_details")
public class AgentDetails {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "agent_code", nullable = false, length = 16)
private String agentCode;
@Column(name = "reporter_code", nullable = false, length = 16)
private String reporterCode;
@Column(name = "branch", nullable = false, length = 32)
private String branch;
}
public interface AgentDetailsRepository extends JpaRepository<AgentDetails, Long>
{
AgentDetails findByAgentCode(String agentCode);
List<AgentDetails> findByReporterCode(String reporterCode);
}
uj5u.com熱心網友回復:
你可以試試這個(公用表運算式):例如:'1011'作為你的動態父節點。
with recursive
cte
(childs)
as
( select agent_code as childs
from agent_details where agent_code = '1011'
union all
select g.agent_code as childs
from cte a
join agent_details g
on a.childs = g.reporter_code
)
select childs as parent_and_childs from cte;
然后在您的存盤庫中創建一個接收List<String>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424543.html
上一篇:如何按fk_name過濾搜索?
