Jpa 使用子id获取容器实体
我想获取分支对象,其中叶(使用其ID)属于该对象 如果我只有叶ID,那么获取分支的正确方法是什么?我想遍历数据库中的所有分支,得到一个包含看起来很糟糕的叶子ID的分支Jpa 使用子id获取容器实体,jpa,spring-data,Jpa,Spring Data,我想获取分支对象,其中叶(使用其ID)属于该对象 如果我只有叶ID,那么获取分支的正确方法是什么?我想遍历数据库中的所有分支,得到一个包含看起来很糟糕的叶子ID的分支 @Entity public class Branch { @Id @GeneratedValue Long id; @OneToMany @JoinColumn(name = "branch_id") private List<Leaf> leaves } @Entity public
@Entity
public class Branch {
@Id
@GeneratedValue
Long id;
@OneToMany
@JoinColumn(name = "branch_id")
private List<Leaf> leaves
}
@Entity
public class Leaf {
@Id
@GeneratedValue
Long id;
private String name;
}
@Service
public class BranchService {
private final BranchRepository branchRepository;
@Autowired
public BranchService(BranchRepository branchRepository) {
branchRepository = branchRepository;
}
public Tree getBranchByLeaf(Long leafId){
// ??
}
}
@实体
公营部门{
@身份证
@生成值
长id;
@独身癖
@JoinColumn(name=“分支机构id”)
私人名单离开
}
@实体
公共类叶{
@身份证
@生成值
长id;
私有字符串名称;
}
@服务
公共类分支服务{
私人最终分行存款分行存款;
@自动连线
公共分支服务(分支机构分支机构分支机构){
BranchRespository=BranchRespository;
}
公共树getBranchByLeaf(长叶ID){
// ??
}
}
尝试以下方法:
public interface BranchRepository extends JpaRepository<Branch, Long> {
@Query("select b from Branch b join b.leaves l where l.id = ?1")
List<Branch> getByLeafId(Long leafId);
}
@Service
public class BranchService {
private final BranchRepository branchRepository;
@Autowired
public BranchService(BranchRepository branchRepository) {
branchRepository = branchRepository;
}
public List<Branch> getByLeafId(Long leafId){
return branchRepository.getByLeafId(Long leafId);
}
}
public interface BranchRepository扩展了JpaRepository{
@查询(“从分支b中选择b加入b.l,其中l.id=?1”)
列表getByLeafId(长叶ID);
}
@服务
公共类分支服务{
私人最终分行存款分行存款;
@自动连线
公共分支服务(分支机构分支机构分支机构){
BranchRespository=BranchRespository;
}
公共列表getByLeafId(长叶ID){
return branchrespository.getByLeafId(长叶ID);
}
}
您没有错过“。。。b、 离开l…?@LemuelNabong立即尝试-我在分支之后错过了别名“b”
…)