2013-11-28 8 views
16

私はどこかで愚かな間違いのために立ち往生していますが、わかりません!Hibernate:CurrentSessionContextは設定されていません

、私はこの質問を参照している4.2.6

Hibernateは、たとえば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」を使用すると、 tは例外です(コンソール上の出力さえも以前の実行より大きいことに注意してください) - クラス 'ThreadLocalSessionContext'がバージョン3.0には存在しませんでしたか?(APIで検索しようとしましたがこのサイトは、古いAPIを持っていません)

今私は、DTDについての不審なよ - 地元のものは、システムが指定されていても、無視されている間にHibernate 3.0のjarファイルは、どこかで言及されていますか?インターネットアクセスが制限されたマシン上でコードを実行している間、前の例外(現在のコンテキストなし)を取得しました。私は私の個人的なノートパソコンで同じことを実行した場合でも:

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(); 

を変更してみてください:私の知る限り1として

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

これは、問題を解決しますが、他を発生させます。このように使用すると、オープンセッションが多すぎないように、セッションを手動でクローズする必要があります。 – adam0404

41

、あなたの設定がために適切ではありません現在のセッション。最後の段落の最後の行を読んで、特別

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

:代わりに、この方法の詳細については

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

使用

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

のは、このリンクをご覧ください。

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 'org.hibernate.context.ThreadLocal SessionContext'の代わりに' thread'だけを書くと、どんな例外がありますか? – RAS

+0

投稿を編集しました - 別のマシンで違った動作をしています。 –

4

あなたのプロパティを変更する必要があります:私は知っている

org.hibernate.context.internal.ThreadLocalSessionContext にスレッド

+0

スレッド、スレッドではない – rberla

1

これは古い投稿ですが、誰かが私のようにここに来る場合に備えて - これは私です作業秒:

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

そして、次のステップは、怒鳴るようなものになるだろう:今すぐ

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; 
} 

} 

これを起動するには、非常に簡単です。また必ずhibernate.cfgことを確認

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

.xmlはsrc/main/resourcesフォルダーにあります。あなたの代わりに org.hibernate.context.ThreadLocalSessionContextorg.hibernate.context.internal.ThreadLocalSessionContextを使用する必要が休止4.xの

+0

これにはまだ問題があります。エラーを取得するorg.hibernate.HibernateException:CurrentSessionContextが設定されていません! – mikebertiean

+0

[OK]を、私は完全なスタックトレースを見ることができないので何が起こっているのかわからないが、多分この投稿はあなたを助けるでしょう:http://stackoverflow.com/questions/7281045/why-do-i-get-org-hibernate -hibernateexception-no-currentsessioncontext-configur – Marco

1

しかし、あなただけのスレッドなくスレッドにプロパティを設定することができます。

<property name="hibernate.current_session_context_class">thread</property> 
関連する問題