Hibernate5

HQL

基本查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
basic_hql();
}

private static void basic_hql() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

String hql1 = "from User"; //简单写法
String hql2 = "from me.yanrs.domain.User"; //完整写法
String hql3 = "from java.lang.Object"; //查询到

Query createQuery = session.createQuery(hql1);
System.out.println(createQuery.list());

Query createQuery2 = session.createQuery(hql2);
System.out.println(createQuery2.list());

Query createQuery3 = session.createQuery(hql3);
System.out.println(createQuery3.list());

tx.commit();
}
}

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
orderby_hql();
}

private static void orderby_hql() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

String hql = "from me.yanrs.domain.User order by user_id asc";
String hql2 = "from me.yanrs.domain.User order by user_id desc";

Query query = session.createQuery(hql);
Query query2 = session.createQuery(hql2);

System.out.println(query.list());
System.out.println(query2.list());

tx.commit();
}
}

条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.Where;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
Where_hql();
}

private static void Where_hql() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

String hql = "from me.yanrs.domain.User where user_id=?";
String hsql2 = "from me.yanrs.domain.User where user_id=:id";

Query query = session.createQuery(hql);
Query query2 = session.createQuery(hsql2);

query.setParameter(0, 1l);
query2.setParameter("id", 1l);

System.out.println(query.list());
System.out.println(query2.list());

tx.commit();
}
}

分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.Where;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
fenye_hql();
}

private static void fenye_hql() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();

String hql = "from me.yanrs.domain.User";
Query query = session.createQuery(hql);

query.setFirstResult(0);
query.setMaxResults(5);

System.out.println(query.list());
transaction.commit();
}
}

聚合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.Where;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
hql_count();
}

private static void hql_count() {
//count 计数
//sum 求和
//avg 平均数
//max 最大值
//min 最小值

Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();

String hql = "select count(*) from me.yanrs.domain.User";
String hql2 = "select sum(user_id) from me.yanrs.domain.User";
String hql3 = "select avg(user_id) from me.yanrs.domain.User";
String hql4 = "select max(user_id) from me.yanrs.domain.User";
String hql5 = "select min(user_id) from me.yanrs.domain.User";

Query query = session.createQuery(hql);
Query query2 = session.createQuery(hql2);
Query query3 = session.createQuery(hql3);
Query query4 = session.createQuery(hql4);
Query query5 = session.createQuery(hql5);

System.out.println(query.uniqueResult());
System.out.println(query2.uniqueResult());
System.out.println(query3.uniqueResult());
System.out.println(query4.uniqueResult());
System.out.println(query5.uniqueResult());

transaction.commit();
}
}

字段查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package me.yanrs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
//hql_count();
hql_field_select();
}

private static void hql_field_select() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();

String hql = "select user_id, user_name from me.yanrs.domain.User";
String hql2 = "select new User(user_id,user_name) from me.yanrs.domain.User"; //使用这个的时候实体类要有构造方法,且构造方法的参数和这个要一样

Query query = session.createQuery(hql);
Query query2 = session.createQuery(hql2);

System.out.println(query.list());
System.out.println(query2.list());

transaction.commit();
}
}

内连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package me.yanrs.service;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
//hql_count();
//hql_field_select();
//inner_join();
}

private static void inner_join() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
String hql = "from User u inner join u.roles";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();

for (Object[] arr : list) {
System.out.println(Arrays.toString(arr));
}
transaction.commit();
}
}

迫切内连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package me.yanrs.service;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
//hql_count();
//hql_field_select();
//inner_join();
inner_join_fetch();
}

private static void inner_join_fetch() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
String hql = "from User u inner join fetch u.roles";
Query query = session.createQuery(hql);
System.out.println(query.list());
transaction.commit();
}
}

区别:使用内连接返回list中每部分是数组,迫切内连接返回list每部分是对象

左外连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package me.yanrs.service;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
//hql_count();
//hql_field_select();
//inner_join();
//inner_join_fetch();
left_join();
}

private static void left_join() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
String hql = "from User u left join u.roles";
Query query = session.createQuery(hql);
System.out.println(query.list());
tx.commit();
}
}

右外连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package me.yanrs.service;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import me.yanrs.utils.HibernateUtils;

public class HQL_Day4 {
public static void main(String[] args) {
//basic_hql();
//orderby_hql();
//Where_hql();
//fenye_hql();
//hql_count();
//hql_field_select();
//inner_join();
//inner_join_fetch();
//left_join();
right_join();
}

private static void right_join() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
String hql = "from User u right join u.roles";
Query query = session.createQuery(hql);
System.out.println(query.list());
tx.commit();
}
}

Criteria

基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package me.yanrs.service;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
bacic_select();
}

private static void bacic_select() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);

System.out.println(criteria.list());
transaction.commit();
}
}

条件查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package me.yanrs.service;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
//bacic_select();
Where_select();
}

private static void Where_select() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();

Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.idEq(1l));

System.out.println(criteria.list());
transaction.commit();
}
}

分页查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package me.yanrs.service;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
//bacic_select();
//Where_select();
fenye();
}

private static void fenye() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(User.class);
criteria.setFirstResult(0);
criteria.setMaxResults(5);
System.out.println(criteria.list());

tx.commit();
}
}

排序查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package me.yanrs.service;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
//bacic_select();
//Where_select();
//fenye();
order_select();
}

private static void order_select() {
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();

Criteria criteria = session.createCriteria(User.class);
criteria.addOrder(Order.asc("user_id"));

System.out.println(criteria.list());
transaction.commit();
}
}

聚合查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package me.yanrs.service;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
//bacic_select();
//Where_select();
//fenye();
//order_select();
select_count();
}

private static void select_count() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(User.class);
//criteria.setProjection(Projections.rowCount());
//criteria.setProjection(Projections.max("user_id"));
//criteria.setProjection(Projections.min("user_id"));
criteria.setProjection(Projections.avg("user_id"));

System.out.println(criteria.uniqueResult());
tx.commit();
}
}

离线查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package me.yanrs.service;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import me.yanrs.domain.User;
import me.yanrs.utils.HibernateUtils;

public class Criteria_Day4 {
public static void main(String[] args) {
//bacic_select();
//Where_select();
//fenye();
//order_select();
//select_count();
detached();
}

private static void detached() {
// 模拟web层或者service层

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
detachedCriteria.add(Restrictions.idEq(1l));

//模拟dao层
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);

System.out.println(criteria.list());
transaction.commit();
}
}