0

我有两个 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>
4

1 回答 1

1

session.getCurrentSession()自动为您关闭事务/会话。如果您查看API,他们会说如果您调用session.openSession(); 您必须在 finally 块中手动关闭会话。

对于惰性属性的问题,您必须在会话中获取惰性属性,即,当事务打开时/就在关闭会话之前。在您的情况下,在调用 commit() 之前。如果在执行该代码后调用惰性属性,您将收到该错误。

为了避免这个问题,我通常会编写几个服务(针对同一个父对象)来加载父对象的不同惰性项并关闭会话。或者您可以将获取类型设置为eager.

于 2014-05-19T15:57:06.340 回答