免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
hibernate之臟數(shù)據(jù)檢查
hibernate臟數(shù)據(jù)檢查

什么是臟數(shù)據(jù)?臟數(shù)據(jù)并不是廢棄和無(wú)用的數(shù)據(jù),而是狀態(tài)前后發(fā)生變化的數(shù)據(jù)。我們看下面的代碼:
Transaction tx=session.beginTransaction();
//從數(shù)據(jù)庫(kù)中加載符合條件的數(shù)據(jù)
User user=(User)session.load(User.class,”1”);
//改變了user對(duì)象的姓名屬性,此時(shí)user對(duì)象成為了所謂的“臟數(shù)據(jù)”
user.setName(“zx”);
tx.commit();
當(dāng)事務(wù)提交時(shí),Hibernate會(huì)對(duì)session中的PO(持久化對(duì)象)進(jìn)行檢測(cè),判斷持久化對(duì)象的狀態(tài)是否發(fā)生了改變,如果發(fā)生了改變就會(huì)將改變更新到數(shù)據(jù)庫(kù)中,這種判斷持久化對(duì)象狀態(tài)稱(chēng)為臟查詢(xún)。
--------------------------------------------------------------------------------
禁止臟數(shù)據(jù)檢查
--------------------------------------------------------------------------------
pom.xml:
view plaincopy to clipboardprint?
01.<project xmlns="http://maven.apache.org/POM/4.0.0 03.    <modelVersion>4.0.0</modelVersion> 
04.    <groupId>hibernateTest</groupId> 
05.    <artifactId>hibernateTest</artifactId> 
06.    <version>1.0-SNAPSHOT</version> 
07.    <packaging>jar</packaging> 
08.    <name>hibernateTest</name> 
09.    <url>http://maven.apache.org</url> 
10.    <dependencies> 
11.        <dependency> 
12.            <groupId>junit</groupId> 
13.            <artifactId>junit</artifactId> 
14.            <version>3.8.1</version> 
15.            <scope>test</scope> 
16.        </dependency> 
17.        <dependency> 
18.            <groupId>org.hibernate</groupId> 
19.            <artifactId>hibernate-core</artifactId> 
20.            <version>3.3.1.GA</version> 
21.        </dependency> 
22.        <dependency> 
23.            <groupId>org.slf4j</groupId> 
24.            <artifactId>slf4j-nop</artifactId> 
25.            <version>1.5.2</version> 
26.        </dependency> 
27.        <dependency> 
28.            <groupId>javassist</groupId> 
29.            <artifactId>javassist</artifactId> 
30.            <version>3.4.GA</version> 
31.        </dependency> 
32.        <dependency> 
33.            <groupId>org.hibernate</groupId> 
34.            <artifactId>hibernate-proxool</artifactId> 
35.            <version>3.3.1.GA</version> 
36.        </dependency> 
37.        <dependency> 
38.            <groupId>com.oracle</groupId> 
39.            <artifactId>ojdbc14</artifactId> 
40.            <version>10.2.0.3.0</version> 
41.            <scope>runtime</scope> 
42.        </dependency> 
43.    </dependencies> 
44.    <build> 
45.        <finalName>hibernateTest</finalName> 
46.        <resources> 
47.            <resource> 
48.                <directory>src/main/resources</directory> 
49.            </resource> 
50.            <resource> 
51.                <directory>src/main/java</directory> 
52.                <excludes> 
53.                    <exclude>**/*.java</exclude> 
54.                </excludes> 
55.            </resource> 
56.        </resources> 
57.        <plugins> 
58.            <plugin> 
59.                <artifactId>maven-compiler-plugin</artifactId> 
60.                <configuration> 
61.                    <source>1.6</source> 
62.                    <target>1.6</target> 
63.                    <encoding>UTF-8</encoding> 
64.                </configuration> 
65.            </plugin> 
66.        </plugins> 
67.    </build> 
68.</project> 
<project xmlns="
02.<something-else-entirely> 
03.    <proxool> 
04.        <!-- 連接池的別名  --> 
05.        <alias>proxool</alias> 
06.        <!-- proxool只能管理由自己產(chǎn)生的連接  --> 
07.        <driver-url>jdbc:oracle:thin:@localhost:1521:XE</driver-url> 
08.        <!--JDBC驅(qū)動(dòng)程序--> 
09.        <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> 
10.        <!-- 用戶(hù)名與密碼 --> 
11.        <driver-properties> 
12.            <property name="user" value="system" /> 
13.            <property name="password" value="password" /> 
14.        </driver-properties> 
15.        <!-- 允許最大連接數(shù) ,默認(rèn)是15,這里設(shè)置為20--> 
16.        <maximum-connection-count>20</maximum-connection-count> 
17.        <!-- 最小連接數(shù) ,默認(rèn)是5,其實(shí)可以不用聲明它--> 
18.        <minimum-connection-count>5</minimum-connection-count> 
19.        <!-- 測(cè)試連接的sql語(yǔ)句 --> 
20.        <house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql> 
21.    </proxool> 
22.</something-else-entirely> 
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
 <proxool>
  <!-- 連接池的別名  -->
  <alias>proxool</alias>
  <!-- proxool只能管理由自己產(chǎn)生的連接  -->
  <driver-url>jdbc:oracle:thin:@localhost:1521:XE</driver-url>
  <!--JDBC驅(qū)動(dòng)程序-->
  <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
  <!-- 用戶(hù)名與密碼 -->
  <driver-properties>
   <property name="user" value="system" />
   <property name="password" value="password" />
  </driver-properties>
  <!-- 允許最大連接數(shù) ,默認(rèn)是15,這里設(shè)置為20-->
  <maximum-connection-count>20</maximum-connection-count>
  <!-- 最小連接數(shù) ,默認(rèn)是5,其實(shí)可以不用聲明它-->
  <minimum-connection-count>5</minimum-connection-count>
  <!-- 測(cè)試連接的sql語(yǔ)句 -->
  <house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql>
 </proxool>
</something-else-entirely>
 
resources/hibernate.cfg.xml:
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?> 
02.<!DOCTYPE hibernate-configuration  
03.    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
04.    "
05.<hibernate-configuration> 
06.    <session-factory name="sessionFactory"> 
07.        <property name="hibernate.connection.provider_class"> 
08.            org.hibernate.connection.ProxoolConnectionProvider  
09.        </property> 
10.        <property name="hibernate.proxool.pool_alias">proxool</property> 
11.        <property name="hibernate.proxool.xml">proxool.xml</property> 
12.        <!-- 輸出sql --> 
13.        <property name="hibernate.show_sql">true</property> 
14.        <!-- 格式化sql --> 
15.        <property name="hibernate.format_sql">true</property> 
16.        <!--指定數(shù)據(jù)庫(kù)適配器 --> 
17.        <property name="dialect"> org.hibernate.dialect.OracleDialect </property> 
18.        <!-- 映射文件 --> 
19.        <mapping resource="hibernateTest/Student.hbm.xml" /> 
20.    </session-factory> 
21.</hibernate-configuration> 
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "
<hibernate-configuration>
 <session-factory name="sessionFactory">
  <property name="hibernate.connection.provider_class">
   org.hibernate.connection.ProxoolConnectionProvider
  </property>
  <property name="hibernate.proxool.pool_alias">proxool</property>
  <property name="hibernate.proxool.xml">proxool.xml</property>
  <!-- 輸出sql -->
  <property name="hibernate.show_sql">true</property>
  <!-- 格式化sql -->
  <property name="hibernate.format_sql">true</property>
  <!--指定數(shù)據(jù)庫(kù)適配器 -->
  <property name="dialect"> org.hibernate.dialect.OracleDialect </property>
  <!-- 映射文件 -->
  <mapping resource="hibernateTest/Student.hbm.xml" />
 </session-factory>
</hibernate-configuration>
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",這個(gè)參數(shù)。Hibernate進(jìn)行一些優(yōu)化,避免臟檢查.
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  
03.    "
04.<hibernate-mapping> 
05.    <class name="hibernateTest.Student" table="STUDENT" mutable="false"> 
06.        <id name="id" column="ID" type="int"> 
07.            <generator class="sequence"> 
08.                <!-- seq_student就是表student的主鍵自增的sequence --> 
09.                <param name="sequence">seq_student</param> 
10.            </generator> 
11.        </id> 
12.        <property name="name" column="name" type="string"/> 
13.        <property name="address" column="address" type="string"/> 
14.        <property name="age" column="age" type="int"/> 
15.    </class> 
16.</hibernate-mapping> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
 "
<hibernate-mapping>
 <class name="hibernateTest.Student" table="STUDENT" mutable="false">
  <id name="id" column="ID" type="int">
   <generator class="sequence">
    <!-- seq_student就是表student的主鍵自增的sequence -->
    <param name="sequence">seq_student</param>
   </generator>
  </id>
  <property name="name" column="name" type="string"/>
  <property name="address" column="address" type="string"/>
  <property name="age" column="age" type="int"/>
 </class>
</hibernate-mapping>
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.        //改變狀態(tài),使之成為臟數(shù)據(jù),因?yàn)樵O(shè)置了 mutable="false"這個(gè)參數(shù),所以不會(huì)臟查詢(xún),這樣數(shù)據(jù)就不會(huì)更新  
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);
  //改變狀態(tài),使之成為臟數(shù)據(jù),因?yàn)樵O(shè)置了 mutable="false"這個(gè)參數(shù),所以不會(huì)臟查詢(xún),這樣數(shù)據(jù)就不會(huì)更新
  stu.setName("kk000000000000");
  transaction1.commit();
  session.close();
  
 }
}
 
輸出的sql結(jié)果為:
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" ,或者設(shè)置mutable="true"也可以。
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  
03.    "
04.<hibernate-mapping> 
05.    <class name="hibernateTest.Student" table="STUDENT"> 
06.        <id name="id" column="ID" type="int"> 
07.            <generator class="sequence"> 
08.                <!-- seq_student就是表student的主鍵自增的sequence --> 
09.                <param name="sequence">seq_student</param> 
10.            </generator> 
11.        </id> 
12.        <property name="name" column="name" type="string"/> 
13.        <property name="address" column="address" type="string"/> 
14.        <property name="age" column="age" type="int"/> 
15.    </class> 
16.</hibernate-mapping> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
 "
<hibernate-mapping>
 <class name="hibernateTest.Student" table="STUDENT">
  <id name="id" column="ID" type="int">
   <generator class="sequence">
    <!-- seq_student就是表student的主鍵自增的sequence -->
    <param name="sequence">seq_student</param>
   </generator>
  </id>
  <property name="name" column="name" type="string"/>
  <property name="address" column="address" type="string"/>
  <property name="age" column="age" type="int"/>
 </class>
</hibernate-mapping>
輸出的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=? 
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Hibernate連接池配置
【Hibernate總結(jié)系列】hibernate.cfg.xml配置 - 阿蜜果 - Bl...
Hibernate應(yīng)用系列之五配置連接池篇
hibernate的二級(jí)緩存
Java配置Hibernate詳細(xì)教程_Hibernate_Java中文網(wǎng)
Hibernate入門(mén)教程(純Eclipse版)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服