我有两个 jsps 试图访问这个数据库例程(通过操作类)。当我没有 try/catch/finally 例程并且没有提交事务或关闭会话时,我可以获得结果,但只是第一次。当下一个 jsp 尝试访问它们时,我得到一个nested transactions not supported
,因为我没有关闭会话。
当我关闭会话时,session.close()
我得到一个session was already closed error
. 当我使用HibernateUtil.close()
(因为我阅读我应该关闭 threadLocale)时,我得到了LazyInitializationException: could not initialize proxy - no Session
.
我需要在第一个数据库命中后保存/保留列表吗?或者从惰性加载变为急切加载?即使在阅读了它们之后,我也不完全理解它们如何应用于这个实例,并且不知道eager
如果我确实需要如何指定加载。我已经看到了这个注释fetch = FetchType.LAZY
的用法,但是我如何使用映射标签来指定渴望?
DAO 交易
public List<Flights> getflights(String departure, String destination)
{
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
List<Flights> flist = null;
if(session ==null){session = HibernateUtil.getSessionFactory().getCurrentSession();}
Transaction tx = session.beginTransaction();
try
{
String hql = "from Flights as f Where f.destinationsByDepartureCode = :departureCode AND f.destinationsByDestinationCode = :destinationCode";
Query q = session.createQuery(hql);
q.setParameter("departureCode", departure);
q.setParameter("destinationCode", destination);
flist = (List<Flights>) q.list();
tx.commit();
tx = null;
}
catch (Exception e)
{
tx.rollback();
e.printStackTrace();
}
finally{
// session.close();
HibernateUtil.closeSession(); // diff to above. this closes threadlocale.
}
return flist;
}
航班地图
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 07/05/2014 11:35:38 PM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class catalog="seng3150" name="model.hibernate.Flights" table="flights">
<id name="flightid" type="java.lang.Integer">
<column name="flightid"/>
<generator class="identity"/>
</id>
<many-to-one class="model.hibernate.Destinations" fetch="select" name="destinationsByStopOverCode">
<column length="3" name="StopOverCode"/>
</many-to-one>
<many-to-one class="model.hibernate.Destinations" fetch="select" name="destinationsByDestinationCode">
<column length="3" name="DestinationCode" not-null="true"/>
</many-to-one>
<many-to-one class="model.hibernate.Airlines" fetch="select" name="airlines">
<column length="2" name="AirlineCode" not-null="true"/>
</many-to-one>
<many-to-one class="model.hibernate.Destinations" fetch="select" name="destinationsByDepartureCode">
<column length="3" name="DepartureCode" not-null="true"/>
</many-to-one>
<many-to-one class="model.hibernate.Planetype" fetch="select" name="planetype">
<column length="20" name="PlaneCode" not-null="true"/>
</many-to-one>
<property name="flightNumber" type="string">
<column length="6" name="FlightNumber" not-null="true"/>
</property>
<property name="departureTime" type="timestamp">
<column length="19" name="DepartureTime" not-null="true"/>
</property>
<property name="arrivalTimeStopOver" type="timestamp">
<column length="19" name="ArrivalTimeStopOver"/>
</property>
<property name="departureTimeStopOver" type="timestamp">
<column length="19" name="DepartureTimeStopOver"/>
</property>
<property name="arrivalTime" type="timestamp">
<column length="19" name="ArrivalTime" not-null="true"/>
</property>
<property name="duration" type="int">
<column name="Duration" not-null="true"/>
</property>
<property name="durationSecondLeg" type="java.lang.Integer">
<column name="DurationSecondLeg"/>
</property>
<set fetch="select" inverse="true" lazy="true" name="bookingses" table="bookings">
<key>
<column name="flightid" not-null="true"/>
</key>
<one-to-many class="model.hibernate.Bookings"/>
</set>
</class>
</hibernate-mapping>