2017-01-18 50 views
2

テーブルスノーボードからすべての情報を取得しようとするWebアプリケーションを実行するときに問題があり、リストに入れてからxHtmlに出力します。しかし、私はこの例外を下にします。Hibernate、ネストされたトランザクションはサポートされていません

org.hibernate.TransactionException:ネストされたトランザクションがサポートされていない

事は、私は、この例外はので、いくつかの説明をいただければ幸いですなぜ起こるか見当もつかないということです。また、コード内に問題が見つかった場合は、それは素晴らしいでしょう。

これは例外です。

HibernateUtilの

public class HibernateUtil { 

    private static final SessionFactory sessionFactory; 

    static { 



     try { 
      // Create the SessionFactory from standard (hibernate.cfg.xml) 
      // config file. 
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Log the exception. 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 

SnowHelper、helperClassの

public class SnowHelper { 

    Session session = null; 

    public SnowHelper() { 
     this.session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    } 

    public List getSnowboards() { 
     List<Snowboard> snowboardList = null; 

     try { 
      Transaction tx = session.beginTransaction(); 
      Query q = session.createQuery("from Snowboard"); 
      snowboardList = (List<Snowboard>) q.list(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return snowboardList; 
    } 
} 

HibernateCfg

<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://cpsrv01.misshosting.com:3306/etvffqgz_snowshop</property> 
    <property name="hibernate.connection.username">etvffqgz_user</property> 
    <property name="hibernate.connection.password">759486456</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property> 
    <mapping resource="Hibernate/Account.hbm.xml"/> 
    <mapping resource="Hibernate/Snowboard.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

SnowboardBean、ManagedBeanクラス

@Named(value = "snowboardBean") 
@Dependent 
public class SnowboardBean { 

    private List<Snowboard> snowList; 
    private SnowHelper snow; 
    /** 
    * Creates a new instance of SnowboardBean 
    */ 
    public SnowboardBean() { 
     snowList = new ArrayList<>(); 
     snow = new SnowHelper(); 

     snowList = snow.getSnowboards(); 
    } 

    /** 
    * @return the snowList 
    */ 
    public List<Snowboard> getSnowList() { 
     return snowList; 
    } 

} 
ここでは、このビットで

答えて

1

は...いくつかの他は、まだコミットされていない間、最終的にそれらのいずれかが開いている..あなたは、このメソッドを呼び出すあなたは、単にそれを毎回閉じずに、新しいトランザクションを開き、そうでない場合は、あなたの取引

Transaction tx = session.beginTransaction(); 
Query q = session.createQuery("from Snowboard"); 
snowboardList = (List<Snowboard>) q.list(); 
tx.commit(); 

をコミット。

(EJBのSpringによって提供される)「コンテナ管理トランザクション」を使用していた場合、トランザクションを明示的にコミットする心配はありません。ここでは、「拡張トランザクション管理」を使用しており、自分でそれを処理する必要があります。

+0

ありがとうございました!それがそれを解決しました。私が従ったガイドには何らかの理由でそこにその行がなかった。 –

+0

完了、ありがとうございました! –

関連する問題