我在我的项目中使用了hibernate:config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@oracle...</property>
<property name="hibernate.connection.username">name</property>
<property name="hibernate.connection.password">passwd</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<mapping resource="db/Sanitka.hbm.xml"/>
<mapping resource="db/Sanitari.hbm.xml"/>
<mapping resource="db/Nemocnica.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的帮助文件看起来像这样
public class helper {
private static Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
public static List<Sanitka> getNemocnicas() {
List<Nemocnica> filmList = null;
org.hibernate.Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery ("from Nemocnica");
filmList = (List<Nemocnica>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return filmList;
}
public static List<Nemocnica> getSanitkas() {
List<Nemocnica> filmList = null;
org.hibernate.Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery ("from Sanitka");
filmList = (List<Nemocnica>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return filmList;
}
}
我正在建立宁静的api,
/nemocnica
route调用getNemocnicas()
和/sanitkas
路由调用getSanitkas()
问题在于,当我转到/nemocnica
时,会调用getNemocnicas()并从数据库返回查询,但在此之后我想转到/sanitkas
它抛出
org.hibernate.TransactionException:不支持嵌套事务
错误。我读到它是bcs我没有关闭之前交易的会话。但是当我添加
if( tx != null)
tx.commit()
在这些方法的return语句之前,它抛出了这个错误:
org.hibernate.SessionException:会话已关闭!
然后关闭交易的最佳方式是什么?
感谢您的帮助!