2

我正在研究休眠。我正在测试一个简单的 Java 以在 2 个单独的会话中加载 2 个实体。我加载了第一个 book 对象,让主线程休眠 5s,这期间我在 db 中更改了 book 的名称。之后,第二个会话将再次加载该书。但是书名还是一样的。预计何时也会改变。

请帮忙。我写了一个 servlet 程序,但同样的问题,数据库更改后更新的数据不显示。

这是我的代码:

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.CacheMode;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import org.hibernate.stat.Statistics;

/**
 * @author Guruzu
 * 
 */
public class Launch_3_5 {

    private static SessionFactory sessionFactory;

    public static Session getSession() {
        if (sessionFactory == null) {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        }
        Session hibernateSession = sessionFactory.openSession();
        return hibernateSession;
    }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        }
        return sessionFactory;
    }

    public static void main(String[] args) {
        Session session1 = getSessionFactory().openSession();

        try {

            Transaction tx = session1.beginTransaction();

            Book book1 = (Book) session1.get(Book.class, (long) 1);

            System.out.println("Book: " + book1.getName());


        //  session1.flush();
        } finally {

            session1.close();
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Session session2 = getSessionFactory().openSession();

        try {

            Book book2 = (Book) session2.get(Book.class, (long) 1);
            System.out.println("Book: " + book2.getName());

        } finally {

            session2.close();
        }

    }
}

Book6_1.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Book" table="Book6_1">

        <id name="book_id" type="long" column="BOOK_ID">
            <generator class="increment">
            </generator>
        </id>
        <property name="isbn" type="string">
            <column name="ISBN" length="50" not-null="true" unique="true" />
        </property>
        <property name="name" type="string">
            <column name="BOOK_NAME" length="100" not-null="true" />
        </property>
        <property name="publishDate" type="date" column="PUBLISH_DATE" />
        <property name="price" type="int" column="PRICE" />


    </class>
</hibernate-mapping> 

休眠.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetutorial</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <mapping resource="Book_6_1.hbm.xml" />

    </session-factory>
</hibernate-configuration>
4

1 回答 1

1

您的第二个会话正在访问二级缓存。如果您不希望它这样做,您可以在配置文件 (use_second_level_cache = false) 或映射文件中完全关闭缓存(从 hbm.xml 中删除“缓存”元素)。

或者您可以使用CacheMode.REFRESH将查询更改为始终访问数据库

    Session session2 = getSessionFactory().openSession();
    session2.setCacheMode(CacheMode.REFRESH);
    try {
        Book book2 = (Book) session2.get(Book.class, (long) 1);
    } finally {
        session2.close();
    }
于 2012-10-23T20:19:28.823 回答