肾炎患者禁忌:hibernate之脏数据检查

来源:百度文库 编辑:九乡新闻网 时间:2024/10/02 18:46:22
hibernate脏数据检查

什么是脏数据?脏数据并不是废弃和无用的数据,而是状态前后发生变化的数据。我们看下面的代码:
Transaction tx=session.beginTransaction();
//从数据库中加载符合条件的数据
User user=(User)session.load(User.class,”1”);
//改变了user对象的姓名属性,此时user对象成为了所谓的“脏数据”
user.setName(“zx”);
tx.commit();
当事务提交时,Hibernate会对session中的PO(持久化对象)进行检测,判断持久化对象的状态是否发生了改变,如果发生了改变就会将改变更新到数据库中,这种判断持久化对象状态称为脏查询。--------------------------------------------------------------------------------禁止脏数据检查--------------------------------------------------------------------------------pom.xml:
view plaincopy to clipboardprint?
01.02.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
03.    4.0.0 
04.    hibernateTest 
05.    hibernateTest 
06.    1.0-SNAPSHOT 
07.    jar 
08.    hibernateTest 
09.    http://maven.apache.org 
10.     
11.         
12.            junit 
13.            junit 
14.            3.8.1 
15.            test 
16.       
 
17.         
18.            org.hibernate 
19.            hibernate-core 
20.            3.3.1.GA 
21.       
 
22.         
23.            org.slf4j 
24.            slf4j-nop 
25.            1.5.2 
26.       
 
27.         
28.            javassist 
29.            javassist 
30.            3.4.GA 
31.       
 
32.         
33.            org.hibernate 
34.            hibernate-proxool 
35.            3.3.1.GA 
36.       
 
37.         
38.            com.oracle 
39.            ojdbc14 
40.            10.2.0.3.0 
41.            runtime 
42.       
 
43.   
 
44.     
45.        hibernateTest 
46.         
47.             
48.                src/main/resources 
49.           
 
50.             
51.                src/main/java 
52.                 
53.                    **/*.java 
54.               
 
55.           
 
56.       
 
57.         
58.             
59.                maven-compiler-plugin 
60.                 
61.                    1.6 
62.                    1.6 
63.                    UTF-8 
64.               
 
65.           
 
66.       
 
67.   
 
68.
 
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4.0.0
 hibernateTest
 hibernateTest
 1.0-SNAPSHOT
 jar
 hibernateTest
 http://maven.apache.org
 
  
   junit
   junit
   3.8.1
   test
  

  
   org.hibernate
   hibernate-core
   3.3.1.GA
  

  
   org.slf4j
   slf4j-nop
   1.5.2
  

  
   javassist
   javassist
   3.4.GA
  

  
   org.hibernate
   hibernate-proxool
   3.3.1.GA
  

  
   com.oracle
   ojdbc14
   10.2.0.3.0
   runtime
  

 

 
  hibernateTest
  
   
    src/main/resources
   

   
    src/main/java
    
     **/*.java
    

   

  

  
   
    maven-compiler-plugin
    
     1.6
     1.6
     UTF-8
    

   

  

 


 
resources/proxool.xml:
view plaincopy to clipboardprint?
01. 
02. 
03.     
04.         
05.        proxool 
06.         
07.        jdbc:oracle:thin:@localhost:1521:XE 
08.         
09.        oracle.jdbc.driver.OracleDriver 
10.         
11.         
12.             
13.             
14.       
 
15.         
16.        20 
17.         
18.        5 
19.         
20.        select sysdate from dual 
21.   
 
22.
 


 
  
  proxool
  
  jdbc:oracle:thin:@localhost:1521:XE
  
  oracle.jdbc.driver.OracleDriver
  
  
   
   
  

  
  20
  
  5
  
  select sysdate from dual
 


 
resources/hibernate.cfg.xml:
view plaincopy to clipboardprint?
01. 
02.03.    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
04.    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
05. 
06.     
07.         
08.            org.hibernate.connection.ProxoolConnectionProvider  
09.       
 
10.        proxool 
11.        proxool.xml 
12.         
13.        true 
14.         
15.        true 
16.         
17.        org.hibernate.dialect.OracleDialect  
18.         
19.         
20.   
 
21.
 

    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 
  
   org.hibernate.connection.ProxoolConnectionProvider
  

  proxool
  proxool.xml
  
  true
  
  true
  
   org.hibernate.dialect.OracleDialect
  
  
 


HibernateTest/Student.java:
view plaincopy to clipboardprint?
01.package hibernateTest;  
02.public class Student {  
03.    private int id;  
04.    private String name;  
05.    private String address;  
06.    private int age;  
07.      
08.    public int getId() {  
09.        return id;  
10.    }  
11.    public String getName() {  
12.        return name;  
13.    }  
14.    public String getAddress() {  
15.        return address;  
16.    }  
17.    public int getAge() {  
18.        return age;  
19.    }  
20.    public void setId(int id) {  
21.        this.id = id;  
22.    }  
23.    public void setName(String name) {  
24.        this.name = name;  
25.    }  
26.    public void setAddress(String address) {  
27.        this.address = address;  
28.    }  
29.    public void setAge(int age) {  
30.        this.age = age;  
31.    }  
32.      
33.      
34.} 
package hibernateTest;
public class Student {
 private int id;
 private String name;
 private String address;
 private int age;
 
 public int getId() {
  return id;
 }
 public String getName() {
  return name;
 }
 public String getAddress() {
  return address;
 }
 public int getAge() {
  return age;
 }
 public void setId(int id) {
  this.id = id;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 
}
 
HibernateTest/Student.hbm.xml, 注意 mutable="false",这个参数。Hibernate进行一些优化,避免脏检查.
view plaincopy to clipboardprint?
01. 
02.03.    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
04. 
05.     
06.         
07.             
08.                 
09.                seq_student 
10.           
 
11.       
 
12.         
13.         
14.         
15.   
 
16.
 

 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 
  
   
    
    seq_student
   

  

  
  
  
 


util/HibernateUtil.java:
view plaincopy to clipboardprint?
01.package util;  
02.import org.hibernate.SessionFactory;  
03.import org.hibernate.cfg.Configuration;  
04.public class HibernateUtil {  
05.    private static SessionFactory sessionFactory;  
06.    static{  
07.        try {  
08.            sessionFactory = new Configuration().configure().buildSessionFactory();  
09.        } catch (Throwable e) {  
10.            throw new ExceptionInInitializerError(e);  
11.        }  
12.    }  
13.    public static SessionFactory getSessionFactory(){  
14.        return sessionFactory;  
15.    }  
16.    public static void shutdown(){  
17.        getSessionFactory().close();  
18.    }  
19.} 
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
 private static SessionFactory sessionFactory;
 static{
  try {
   sessionFactory = new Configuration().configure().buildSessionFactory();
  } catch (Throwable e) {
   throw new ExceptionInInitializerError(e);
  }
 }
 public static SessionFactory getSessionFactory(){
  return sessionFactory;
 }
 public static void shutdown(){
  getSessionFactory().close();
 }
}
 
util/StudentManager.java:
view plaincopy to clipboardprint?
01.package util;  
02.import hibernateTest.Student;  
03.import org.hibernate.Session;  
04.import org.hibernate.Transaction;  
05.public class StudentManager {  
06.    public static void main(String[] args) {  
07.        Session session = HibernateUtil.getSessionFactory().openSession();  
08.        Transaction transaction1 = session.beginTransaction();  
09.        Student stu = (Student) session.get(Student.class, 2);  
10.        //改变状态,使之成为脏数据,因为设置了 mutable="false"这个参数,所以不会脏查询,这样数据就不会更新  
11.        stu.setName("kk000000000000");  
12.        transaction1.commit();  
13.        session.close();  
14.          
15.    }  
16.} 
package util;
import hibernateTest.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class StudentManager {
 public static void main(String[] args) {
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction transaction1 = session.beginTransaction();
  Student stu = (Student) session.get(Student.class, 2);
  //改变状态,使之成为脏数据,因为设置了 mutable="false"这个参数,所以不会脏查询,这样数据就不会更新
  stu.setName("kk000000000000");
  transaction1.commit();
  session.close();
  
 }
}
 
输出的sql结果为:
view plaincopy to clipboardprint?
01.Hibernate:   
02.    select  
03.        student0_.ID as ID0_0_,  
04.        student0_.name as name0_0_,  
05.        student0_.address as address0_0_,  
06.        student0_.age as age0_0_   
07.    from  
08.        STUDENT student0_   
09.    where  
10.        student0_.ID=? 
Hibernate:
    select
        student0_.ID as ID0_0_,
        student0_.name as name0_0_,
        student0_.address as address0_0_,
        student0_.age as age0_0_
    from
        STUDENT student0_
    where
        student0_.ID=? --------------------------------------------------------------------------------允许脏查询--------------------------------------------------------------------------------我们只需要改变Student.hbm.xml  ,这里去掉了mutable="false" ,或者设置mutable="true"也可以。
view plaincopy to clipboardprint?
01. 
02.03.    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
04. 
05.     
06.         
07.             
08.                 
09.                seq_student 
10.           
 
11.       
 
12.         
13.         
14.         
15.   
 
16.
 

 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 
  
   
    
    seq_student
   

  

  
  
  
 


输出的sql:
view plaincopy to clipboardprint?
01.Hibernate:   
02.    select  
03.        student0_.ID as ID0_0_,  
04.        student0_.name as name0_0_,  
05.        student0_.address as address0_0_,  
06.        student0_.age as age0_0_   
07.    from  
08.        STUDENT student0_   
09.    where  
10.        student0_.ID=?  
11.Hibernate:   
12.    update  
13.        STUDENT   
14.    set  
15.        name=?,  
16.        address=?,  
17.        age=?   
18.    where  
19.        ID=?