2013-11-28 50 views
16

我被卡住了,因爲一些愚蠢的錯誤,但無法弄清楚!Hibernate:沒有配置CurrentSessionContext

的Hibernate 4.2.6

我都提到了這個問題,多次詢問以前,例如here

的hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 

     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.password">root</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> 
     <property name="hibernate.connection.username">root</property> 

     <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 

     <property name="show_sql">true</property> 

     <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> 

     <mapping resource="Event.hbm.xml"></mapping> 
    </session-factory> 
</hibernate-configuration> 

Event.hbm。 xml

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 

    <class name="com.db.pojo.Event" table="Events"> 

     <id name="id" column="Id"> 
      <generator class="native"></generator> 
     </id> 
     <property name="title" column="Title"></property> 

     <property name="date" column="Date" type="timestamp"></property> 

    </class> 

</hibernate-mapping> 


package com.db.util; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistryBuilder; 

public class HibernateUtil { 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     // TODO Auto-generated method stub 

     Configuration configuration = new Configuration(); 
     configuration.configure("hibernate.cfg.xml"); 

     ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder() 
       .applySettings(configuration.getProperties()); 
     return configuration.buildSessionFactory(serviceRegistryBuilder 
       .buildServiceRegistry()); 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

} 

每當我試試這個:

Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 

我得到:

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) 
    at test.EventManager.createAndStoreEvent(EventManager.java:27) 
    at test.EventManager.main(EventManager.java:17) 

我個人的筆記本電腦執行後可進行編輯

  1. 的代碼運行正常,如果我使用 '線'在cfg文件中
  2. 如果我使用'ThreadLocalSessionContext',我ge t下面的例外(請注意,即使在控制檯上的輸出現在比以前的運行更大) - 3.0版本中是否存在類'ThreadLocalSessionContext'(我試圖在API中找到但是該網站沒有舊API

現在我懷疑有關DTD - 是的Hibernate 3.0瓶,而本地的被忽略的地方被提及,即使指定的系統?在受限制的Internet訪問的計算機上執行代碼時,我得到了以前的異常(無當前上下文)。但是,當我執行同樣的我個人的筆記本電腦:

eventDesciption : Team eventDate : 2013-12-12 
Nov 28, 2013 8:55:59 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} 
Nov 28, 2013 8:55:59 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.2.6.Final} 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: hibernate.cfg.xml 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: resources/Event.hbm.xml 
Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000115: Hibernate connection pool size: 20 
Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000006: Autocommit mode: false 
Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test] 
Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000046: Connection properties: {user=root, password=****} 
Nov 28, 2013 8:56:00 PM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
Nov 28, 2013 8:56:00 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation 
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 
Nov 28, 2013 8:56:00 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
Nov 28, 2013 8:56:00 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
Nov 28, 2013 8:56:00 PM org.hibernate.internal.SessionFactoryImpl buildCurrentSessionContext 
ERROR: HHH000302: Unable to construct current session context [org.hibernate.context.ThreadLocalSessionContext] 
org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.context.ThreadLocalSessionContext] 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:152) 
    at org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1544) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:516) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790) 
    at com.db.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) 
    at com.db.util.HibernateUtil.<clinit>(HibernateUtil.java:9) 
    at test.EventManager.createAndStoreEvent(EventManager.java:27) 
    at test.EventManager.main(EventManager.java:17) 
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.context.ThreadLocalSessionContext 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:319) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Unknown Source) 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149) 
    ... 7 more 

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) 
    at test.EventManager.createAndStoreEvent(EventManager.java:27) 
    at test.EventManager.main(EventManager.java:17) 

回答

8

嘗試改變

Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 

到:

Session session = HibernateUtil.getSessionFactory().openSession(); 
+6

這解決了這個問題,但提出了另一個。如果以這種方式使用,會話必須手動關閉以避免打開太多會話。 – adam0404

41

由於按照我所知,你的配置是不恰當的當前會話。取而代之的

<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocal‌​SessionContext</property> 

使用

<property name="hibernate.current_session_context_class">thread</property> 

欲瞭解更多有關此,請訪問以下鏈接:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session

特別的,閱讀最後一段的最後一行。

對於那些使用Hibernate 4的人。1,

<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property> 

參考:https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/context/internal/ThreadLocalSessionContext.html

+0

在發佈這個問題之前,我曾經提到類似的帖子,並嘗試過兩種方法,問題是別的! –

+0

@KaliyugAntagonist當你只寫'thread'而不是'org.hibernate.context.ThreadLocal SessionContext'時,你會得到什麼異常? – RAS

+0

我已編輯我的帖子 - 我在不同的機器上獲得不同的行爲。 –

4

你需要改變你的財產:

org.hibernate.context.internal.ThreadLocalSessionContext主題

+0

線程,而不是線程 – rberla

1

我知道這是一箇舊帖子,但以防萬一有人像我這樣來到這裏 - This i s工作

package com.spring.util; 

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

public class HibernateUtil { 

private static HibernateUtil instance = new HibernateUtil(); 

private SessionFactory sessionFactory; 

private HibernateUtil(){ 
    this.sessionFactory = buildSessionFactory(); 
} 

private synchronized static SessionFactory buildSessionFactory() { 
    return new Configuration().configure().buildSessionFactory(); 
} 

public static HibernateUtil getInstance() { 
    if(instance == null){ 
     return new HibernateUtil(); 
    } 
    return instance; 
} 

public SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

} 

我們調用這個很簡單:

SessionFactory sessionFactory = HibernateUtil.getInstance().getSessionFactory(); 

,下一步會是這樣的波紋管:

public void saveOrUpdate(MyObject myObj) { 
    Session session = sessionFactory.getCurrentSession(); 
    session.beginTransaction(); 
    session.saveOrUpdate(myObj); 
    session.getTransaction().commit(); 
} 

還要確保hibernate.cfg .xml位於src/main/resources文件夾中。

+0

這仍然有問題。得到錯誤org.hibernate.HibernateException:沒有配置CurrentSessionContext! – mikebertiean

+0

好了,不知道發生了什麼,因爲我不能看到完整的堆棧跟蹤,但也許這篇文章將幫助您:http://stackoverflow.com/questions/7281045/why-do-i-get-org-hibernate -hibernateexception-NO-currentsessioncontext-configur – Marco

1

對Hibernate 4.x版,您需要使用org.hibernate.context.internal.ThreadLocalSessionContext,而不是 org.hibernate.context.ThreadLocalSessionContext

但是,您可以將屬性正好被設置爲線程,而不是主題

<property name="hibernate.current_session_context_class">thread</property> 
相關問題