このデータベース ルーチンに (アクション クラスを介して) アクセスしようとしている 2 つの jsps があります。try/catch/finally ルーチンがなく、トランザクションのコミットやセッションの終了がない場合、結果を取得できるのは初めてです。次のjspがそれらにアクセスしようとするとnested transactions not supported
、セッションを閉じていなかったため、が発生します。
セッションを閉じるとsession.close()
、session was already closed error
. を使用するとHibernateUtil.close()
(スレッドローカルを閉じる必要があるため)を取得し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>