Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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
Mysql Hibernate criteriaQuery-左联接_Mysql_Hibernate_Join - Fatal编程技术网

Mysql Hibernate criteriaQuery-左联接

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

如何使用条件查询应用左连接,我只能在internet上找到内部连接条件

        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
子句,但是如果您显示了数据库结构、一些示例数据和所需的结果,您将得到更好的答案。