2012-08-17 14 views
8

私はJSFアプリケーションを作成しており、それにいくつかの休止状態のものを使用しています。私がやりたいすべてのデータベースにエンティティを保存することですが、私はこの例外得続ける:最初に、私はこの例外を得ていた時org.hibernate.HibernateException:アクティブなトランザクションなしで保存が有効でない

org.hibernate.HibernateException: save is not valid without active transaction 

を:

org.hibernate.HibernateException: No CurrentSessionContext configured! 

その後、私は私が追加する必要があることがわかりましたこれを私の休止状態にしてください:

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

これでこの問題は解決されましたが、上記の問題が解決しました。私はこのようなデータベースにエンティティを保存しています :

public void create(T entity) { 
    getSessionFactory().getCurrentSession().save(entity); 
} 

マイhibernate.cfg.xmlファイルは次のようになります。

<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/online_tests_management</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password">root</property> 
     <property name="hibernate.show_sql">true</property> 
     <property name="hibernate.hbm2ddl.auto">update</property> 
     <property name="hibernate.current_session_context_class">thread</property> 

     <mapping class="com.groupgti.onlinetests.management.db.Service"/> 
    </session-factory> 
</hibernate-configuration> 

私が使用しています:

  • にHibernate-4.1.4を.Final
  • JDK 1.6
  • Tomcat 6
  • JSF 2.0
  • PrimeFaces 3.3.1
  • のMySQL

誰かが問題になる可能性がある場所を知っていますか?

答えて

20

あなたは次のようにあなたの方法を変更してみてください

public void create(T entity) { 
    Session session=getSessionFactory().getCurrentSession(); 
    Transaction trans=session.beginTransaction(); 
    session.save(entity); 
    trans.commit(); 
} 
+0

ありがとうございました。 –

+0

私はSpring AOPを通してSpring HibernateTransactionManagerを使ってトランザクションを設定しています。ログにトランザクションが正しく開始されていることがわかりますが、上記のコードを使用していないため保存に失敗しています。何故ですか? –

3

を呼び出す必要があり:

public void create(T entity) { 
    getSessionFactory().getCurrentSession().beginTransaction(); 
    getSessionFactory().getCurrentSession().save(entity); 
    getSessionFactory().getCurrentSession().endTransaction(); 
} 

あなたの問題を解決する必要があります。

3

はこのようにそれを実行します。

public void create(T entity) { 
     org.hibernate.Session ss= getSessionFactory().getCurrentSession(); 
     Transaction tx=ss.beginTransaction(); 
     ss.save(entity); 
     tx.commit();  
    } 

は、一部を自分で例外処理を行います。

2

私は、これはより堅牢かつ適切であるようにあなたが何かを見つけることだと思う:

Session session = factory.openSession(); 
Transaction tx = null; 
try { 
    tx = session.beginTransaction(); 

    // Do some work like: 
    //session.load(...); 
    //session.persist(...); 
    //session.save(...); 

    tx.commit(); // Flush happens automatically 
} 
catch (RuntimeException e) { 
    tx.rollback(); 
    throw e; // or display error message 
} 
finally { 
    session.close(); 
} 

あなたが取引を「閉じる」ことができないあなたが見ることができるように、あなたは、セッションを閉じることができます。 this hereを読んで、それはあなたにとって有用かもしれません。

+0

注意、 "openSession"は避けてください。適切なメソッドは "getCurrentSession"です。 –

関連する問題