使用子ID获取容器实体

时间:2017-08-13 11:38:12

标签: jpa spring-data

我想获得分支对象,其中 leaf (使用它的ID)属于

获取分支的正确方法是什么,因为我只有 leaf ID?我想循环遍历数据库中的所有分支,并获取包含看起来不好的叶ID的那个

static

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

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);
    }
}
相关问题