jpa api原生JOIN-基于CriteriaQuery的关联

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();

CriteriaQuery query = cb.createQuery(TestStudentPo.class);

Root root = query.from(TestStudentPo.class);
//创建关联关系
Join join = root.join("testClassPo", JoinType.LEFT);

query.select(root).where(cb.equal(join.get("classId"), "1"));

List list = em.createQuery(query).getResultList();

------------

这段JPA API 转为sql为:

------------


SELECT
a.STUDENT_ID AS STUDENT_1_2_,
a.CLASS_ID AS CLASS_ID2_2_,
a.COURSE_ID AS COURSE_I3_2_,
a.STUDENT_NAME AS STUDENT_4_2_,
a.TIME AS TIME5_2_
FROM
test_student a
LEFT OUTER JOIN test_class b ON a.CLASS_ID = b.CLASS_ID
WHERE
b.CLASS_ID =?

最重要的是这个关系要配置好

    @OneToOne
    @JoinColumn(name="CLASS_ID",referencedColumnName = "CLASS_ID")
    private TestClassPo testClassPo;

转载于:https://my.oschina.net/u/3005325/blog/2879089

你可能感兴趣的:(jpa api原生JOIN-基于CriteriaQuery的关联)