2017-01-18 126 views
2

运行我的Web应用程序时,尝试从桌面滑雪板获取所有信息并将其放入列表中,然后我将在xHtml中打印出列表。但是我在下面得到这个例外。不支持休眠,嵌套事务

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,助手类

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

否则,你只需打开一个新的事务而无须在每次关闭它,你调用此方法..最终他们中的一个,而其他一些尚未COMMITED被打开。

如果您使用'容器管理的事务'(由EJB的Spring提供),您不必担心显式提交事务。这里您使用的是“扩展事务管理”,您必须自己处理。

+0

非常感谢!这解决了它。我遵循的指南由于某种原因没有在那里。 –

+0

完成,谢谢你的时间! –