如何使用条件查询应用左连接,我只能在互联网上找到内连接条件。
Criteria cr = this.sessionFactory.getCurrentSession().createCriteria(A.class, "a").createAlias("a.division", "division");
List<A> list = cr.list();
division是A类中的实体,这是返回内部联接结果。我想在部门实体上应用左连接:
select * from A as a left join divisions as division on division.id = a.id;
表A数据:
id name division_id
1 first name 1
3 sec name 2
6 fourth name 2
5 3rd name NULL
表格划分数据:
id type
1 F
2 G
内部联接:
select * from A as a inner join division where a.division_id = division.id
内部联接结果:
+----+-------------+-------------+----+------+
| id | name | division_id | id | type |
+----+-------------+-------------+----+------+
| 1 | first name | 1 | 1 | F |
| 3 | sec name | 2 | 2 | G |
| 6 | fourth name | 2 | 2 | G |
+----+-------------+-------------+----+------+
左连接查询:
select * from A as a left join division on division.id = a.division_id;
左连接结果:
+----+-------------+-------------+------+------+
| id | name | division_id | id | type |
+----+-------------+-------------+------+------+
| 1 | first name | 1 | 1 | F |
| 3 | sec name | 2 | 2 | G |
| 6 | fourth name | 2 | 2 | G |
| 5 | 3rd name | NULL | NULL | NULL |
+----+-------------+-------------+------+------+
答案 0 :(得分:1)
如果您想要左连接,可以在createAlias
中提及。默认值为inner join
Criteria cr = this.sessionFactory.getCurrentSession().createCriteria(A.class, "a")
.createAlias("a.division", "division", CriteriaSpecification.LEFT_JOIN); // Add join type
List<A> list = cr.list();