1. 程式人生 > >關於緩存一些問題

關於緩存一些問題

values doctype before maven ransac epo ner 關於 lin

1.新建一個工具類

技術分享圖片

2.編寫代碼

package cn.happy.day01.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created by CY on 2017/12/23.
 */
public class HibernateUtil {
    private static ThreadLocal<Session> tl=new ThreadLocal<Session>();
    
private static Configuration cfg; private static SessionFactory sessionFactory; static { cfg=new Configuration().configure(); sessionFactory=cfg.buildSessionFactory(); } public static Session getSession(){ Session session=tl.get(); if(session==null
){ session = sessionFactory.openSession(); tl.set(session); } return session; } public static void closeSession(){ Session session = (Session) tl.get(); tl.set(null); session.close(); } }

註意這裏使用的是openSession();

3.大配置小配置:不用解了

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!---->
    <session-factory>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">happy</property>
        <property name="connection.password">happy</property>
        <!--sql 方言-->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!--連接池-->
        <!--<property name="connection.pool_size">1</property>-->
        <!--和當前線程綁定-->
        <property name="current_session_context_class">thread</property>
        <!--echo 打印控制臺語句 shout-->
        <property name="show_sql">true</property>
        <!--格式化代碼-->
        <property name="format_sql">true</property>
        <!--自動更新表結構 createe  先delete表結構 在創建  update直接更新表結構-->
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="cn/happy/day01/entity/Dog.hbm.xml" />
        <mapping resource="cn/happy/day01/entity/Student.hbm.xml" />
    </session-factory>

</hibernate-configuration>
@Entity
public class Student {
    private  Integer sid;
    private  String sname;
    private  Integer sage;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.day01.entity">
    <class name="Student" table="STUDENT" schema="happy">
        <id name="sid"  column="sid">
            <!-- native   數據庫方言的可移植性 -->
            <generator class="native"/>
        </id>
        <property name="sname" column="sname" />
        <property name="sage"  column="sage"/>
    </class>
</hibernate-mapping>

4.數據庫使用的是Oracle

/*
Navicat Oracle Data Transfer
Oracle Client Version : 11.2.0.1.0

Source Server         : happy
Source Server Version : 110200
Source Host           : localhost:1521
Source Schema         : HAPPY

Target Server Type    : ORACLE
Target Server Version : 110200
File Encoding         : 65001

Date: 2017-12-24 22:31:33
*/


-- ----------------------------
-- Table structure for STUDENT
-- ----------------------------
DROP TABLE "HAPPY"."STUDENT";
CREATE TABLE "HAPPY"."STUDENT" (
"SID" NUMBER(10) NOT NULL ,
"SNAME" VARCHAR2(255 CHAR) NULL ,
"SAGE" NUMBER(10) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of STUDENT
-- ----------------------------
INSERT INTO "HAPPY"."STUDENT" VALUES (‘4‘, ‘小紅‘, ‘12‘);

-- ----------------------------
-- Indexes structure for table STUDENT
-- ----------------------------

-- ----------------------------
-- Checks structure for table STUDENT
-- ----------------------------
ALTER TABLE "HAPPY"."STUDENT" ADD CHECK ("SID" IS NOT NULL);

-- ----------------------------
-- Primary Key structure for table STUDENT
-- ----------------------------
ALTER TABLE "HAPPY"."STUDENT" ADD PRIMARY KEY ("SID");

5.使用的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Y2166</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hibernate</artifactId>
    <packaging>war</packaging>
    <name>hibernate Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
            <scope>test</scope>
        </dependency>
        <!--oracle jdbc-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
        <!--hibernate核心jar-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <!--oracle 事務-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <!--mysql數據庫驅動-->
        <dependency>
            <groupId>org.wisdom-framework</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34_1</version>
        </dependency>

    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

6.測試類

數據庫修改前:

技術分享圖片

測試修改

package day02hql;

import cn.happy.day01.entity.Student;
import cn.happy.day01.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * 臟檢查和緩存機制探討
 */
public class mytest20171224_02 {
    Configuration cfg;
    SessionFactory factory;
    Session session;
    Transaction tx;
    @Before
    public void before(){
        cfg=new Configuration().configure();
        factory=cfg.buildSessionFactory();
        session=factory.getCurrentSession();
        tx=session.beginTransaction();
    }


    @Test
    public void t1(){
        //使用update更新數據
        Student student = session.load(Student.class, 4);
        student.setSname("小賤賤");
        session.update(student);
        tx.commit();
        //在數據同步情況下更新數據
        Student student1 = HibernateUtil.getSession().load(Student.class, 4);
        student1.setSname("小軍");
        HibernateUtil.closeSession();
    }
}

數據庫運行後

技術分享圖片

運行結果:

可以看到數據庫確實是更新了

但是控制臺卻打印出來兩條查詢語句

E:\jdk\bin\java -ea -Didea.launcher.port=7535 "-Didea.launcher.bin.path=E:\ideat\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\ideat\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;E:\ideat\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;E:\jdk\jre\lib\charsets.jar;E:\jdk\jre\lib\deploy.jar;E:\jdk\jre\lib\ext\access-bridge-32.jar;E:\jdk\jre\lib\ext\cldrdata.jar;E:\jdk\jre\lib\ext\dnsns.jar;E:\jdk\jre\lib\ext\jaccess.jar;E:\jdk\jre\lib\ext\jfxrt.jar;E:\jdk\jre\lib\ext\localedata.jar;E:\jdk\jre\lib\ext\nashorn.jar;E:\jdk\jre\lib\ext\sunec.jar;E:\jdk\jre\lib\ext\sunjce_provider.jar;E:\jdk\jre\lib\ext\sunmscapi.jar;E:\jdk\jre\lib\ext\sunpkcs11.jar;E:\jdk\jre\lib\ext\zipfs.jar;E:\jdk\jre\lib\javaws.jar;E:\jdk\jre\lib\jce.jar;E:\jdk\jre\lib\jfr.jar;E:\jdk\jre\lib\jfxswt.jar;E:\jdk\jre\lib\jsse.jar;E:\jdk\jre\lib\management-agent.jar;E:\jdk\jre\lib\plugin.jar;E:\jdk\jre\lib\resources.jar;E:\jdk\jre\lib\rt.jar;E:\Y2166\hibernate\target\test-classes;E:\Y2166\hibernate\target\classes;E:\mavon\repository\junit\junit\4.3\junit-4.3.jar;E:\mavon\repository\com\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar;E:\mavon\repository\org\hibernate\hibernate-core\5.0.6.Final\hibernate-core-5.0.6.Final.jar;E:\mavon\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;E:\mavon\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;E:\mavon\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;E:\mavon\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;E:\mavon\repository\org\jboss\jandex\2.0.0.Final\jandex-2.0.0.Final.jar;E:\mavon\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;E:\mavon\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;E:\mavon\repository\org\hibernate\common\hibernate-commons-annotations\5.0.1.Final\hibernate-commons-annotations-5.0.1.Final.jar;E:\mavon\repository\javax\transaction\jta\1.1\jta-1.1.jar;E:\mavon\repository\org\wisdom-framework\mysql-connector-java\5.1.34_1\mysql-connector-java-5.1.34_1.jar;E:\mavon\repository\org\apache\felix\org.apache.felix.ipojo.annotations\1.12.1\org.apache.felix.ipojo.annotations-1.12.1.jar;E:\mavon\repository\org\wisdom-framework\abstract-jdbc-driver\0.5\abstract-jdbc-driver-0.5.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 day02hql.mytest20171224_02,t1
十二月 24, 2017 10:36:14 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.6.Final}
十二月 24, 2017 10:36:14 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 24, 2017 10:36:14 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十二月 24, 2017 10:36:14 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=happy, password=****}
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 24, 2017 10:36:16 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
十二月 24, 2017 10:36:16 下午 org.hibernate.id.UUIDHexGenerator <init>
WARN: HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
十二月 24, 2017 10:36:17 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Hibernate: 
    select
        student0_.sid as sid1_1_0_,
        student0_.sname as sname2_1_0_,
        student0_.sage as sage3_1_0_ 
    from
        happy.STUDENT student0_ 
    where
        student0_.sid=?
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=happy, password=****}
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 24, 2017 10:36:18 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
十二月 24, 2017 10:36:18 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Hibernate: 
    select
        student0_.sid as sid1_1_0_,
        student0_.sname as sname2_1_0_,
        student0_.sage as sage3_1_0_ 
    from
        happy.STUDENT student0_ 
    where
        student0_.sid=?

Process finished with exit code 0

?待討論

關於緩存一些問題