Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 使用子id获取容器实体_Jpa_Spring Data - Fatal编程技术网

Jpa 使用子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

我想获取分支对象,其中(使用其ID)属于该对象

如果我只有叶ID,那么获取分支的正确方法是什么?我想遍历数据库中的所有分支,得到一个包含看起来很糟糕的叶子ID的分支

@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”
…)