Hibernate---QueryCriteriaSQLQuery

Posted Angelye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate---QueryCriteriaSQLQuery相关的知识,希望对你有一定的参考价值。

一、核心配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 8         <property name="hibernate.connection.password">root</property>
 9         <property name="hibernate.connection.url">jdbc:mysql:///test</property>
10         <property name="hibernate.connection.username">root</property>
11         
12         <property name="hibernate.show_sql">true</property>
13         <property name="hibernate.format_sql">true</property>
14         <property name="hibernate.hbm2ddl.auto">update</property>
15         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
16         
17         <property name="current_session_context_class">thread</property>
18 
19         <mapping resource="com/chinasofti/entity/EUser.hbm.xml"/>
20         
21     </session-factory>
22 </hibernate-configuration>

二、实体类

 1 package com.chinasofti.entity;
 2 
 3 public class EUser {
 4     private int id;
 5     private String username;
 6     private String password;
 7 
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUsername() {
15         return username;
16     }
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     public String getPassword() {
21         return password;
22     }
23     public void setPassword(String password) {
24         this.password = password;
25     }
26     @Override
27     public String toString() {
28         return "EUser [id=" + id + ", username=" + username + ", password="
29                 + password + "]";
30     }
31     
32 }

三、映射文件

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2017-8-10 10:27:23 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping>
 6     <class name="com.chinasofti.entity.EUser" table="EUSER">
 7         <id name="id" type="int">
 8             <column name="ID" />
 9             <generator class="native" />
10         </id>
11         <property name="username" type="java.lang.String">
12             <column name="USERNAME" />
13         </property>
14         <property name="password" type="java.lang.String">
15             <column name="PASSWORD" />
16         </property>
17     </class>
18 </hibernate-mapping>

四、封装类

 1 package com.chinasofti.entity;
 2 import org.hibernate.Session;
 3 import org.hibernate.SessionFactory;
 4 import org.hibernate.cfg.Configuration;
 5 
 6 public class HibernateUtil {
 7     static Configuration CONFIGURATION = null;
 8     static SessionFactory SESSION_FACTORY = null;
 9     static Session SESSION = null;
10     static{
11         CONFIGURATION = new Configuration();
12         CONFIGURATION.configure();
13         SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
14         SESSION    = SESSION_FACTORY.openSession();
15     }
16     public static SessionFactory getSessionFactory(){
17         return SESSION_FACTORY;
18     }
19     public static Session openSession(){
20         return SESSION;
21     }
22 }

五、测试类

 1 package com.chinasofti.test;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Criteria;
 6 import org.hibernate.Query;
 7 import org.hibernate.SQLQuery;
 8 import org.hibernate.Session;
 9 import org.hibernate.Transaction;
10 import org.junit.Test;
11 
12 import com.chinasofti.entity.EUser;
13 import com.chinasofti.entity.HibernateUtil;
14 
15 public class TestHibernateTestQuery {
16 
17     @Test
18     public void TestQuery(){
19         Session session = HibernateUtil.openSession();
20         Query query = session.createQuery("from EUser");
21         List<EUser> list = query.list();
22         for (EUser eUser : list) {
23             System.out.println(eUser.getUsername());
24         }
25     }
26 
27     
28     @Test
29     public void TestCriteriaQuery(){
30         Session session = HibernateUtil.openSession();
31         Criteria criteria = session.createCriteria(EUser.class);
32         List<EUser> list = criteria.list();
33         for (EUser eUser : list) {
34             System.out.println(eUser.getUsername());
35         }
36     }
37     @Test
38     public void TestSqlQuery(){
39         Session session = HibernateUtil.openSession();
40         //创建SQLQuery对象
41         SQLQuery sQLQuery = session.createSQLQuery("select * from euser");
42         //添加一个实体类
43         sQLQuery.addEntity(EUser.class);
44         List<EUser> list = sQLQuery.list();
45         for (EUser eUsers : list) {
46             System.out.println(eUsers.getUsername());
47         }
48     }
49     
50     
51     @Test
52     public void testRollback(){
53         Session session=null;
54         //注意:先查询再修改
55         Transaction transaction=null;
56         try {
57             session = HibernateUtil.openSession();
58             transaction = session.beginTransaction();
59             EUser user = session.get(EUser.class, 4);
60             //给user对象设置新的值
61             user.setUsername("滚回来");
62             session.save(user);
63             System.out.println(10/0);
64             transaction.commit();
65             //注意:mysql和oracle数据不一样,如果这里操作的是oracle,这里数据就不会变。因为mysql是自动提交的(默认)
66         } catch (Exception e) {
67             // TODO Auto-generated catch block
68             transaction.rollback();
69         }finally{
70             session.close();
71         }
72     }
73 
74     
75 }

 

之前Hibernate API的使用(Query、Criteria、SQLQuery对象) 没有附代码    今天补上

以上是关于Hibernate---QueryCriteriaSQLQuery的主要内容,如果未能解决你的问题,请参考以下文章