Mysql Hibernate criteriaQuery-左联接
如何使用条件查询应用左连接,我只能在internet上找到内部连接条件Mysql Hibernate criteriaQuery-左联接,mysql,hibernate,join,Mysql,Hibernate,Join,如何使用条件查询应用左连接,我只能在internet上找到内部连接条件 Criteria cr = this.sessionFactory.getCurrentSession().createCriteria(A.class, "a").createAlias("a.division", "division"); List<A> list = cr.list(); 表A数据: id name
Criteria cr = this.sessionFactory.getCurrentSession().createCriteria(A.class, "a").createAlias("a.division", "division");
List<A> list = cr.list();
表A数据:
id name division_id
1 first name 1
3 sec name 2
6 fourth name 2
5 3rd name NULL
表2.分区数据:
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 |
+----+-------------+-------------+----+------+
+----+-------------+-------------+------+------+
| 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 |
+----+-------------+-------------+------+------+
左连接查询:
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 |
+----+-------------+-------------+----+------+
+----+-------------+-------------+------+------+
| 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 |
+----+-------------+-------------+------+------+
如果您想要左加入,可以在
createAlias
中提及。默认值为内部联接
Criteria cr = this.sessionFactory.getCurrentSession().createCriteria(A.class, "a")
.createAlias("a.division", "division", CriteriaSpecification.LEFT_JOIN); // Add join type
List<A> list = cr.list();
Criteria cr=this.sessionFactory.getCurrentSession().createCriteria(A.class,“A”)
.createAlias(“a.division”,“division”,CriteriaSpecification.LEFT_JOIN);//添加联接类型
List您可以在join之后使用where
子句,但是如果您显示了数据库结构、一些示例数据和所需的结果,您将得到更好的答案。