0

我遇到了与 Hibernate + Spring Transaction Manager 相关的问题,在从会话开始事务后查询休眠对象时,所有子实体都为 NULL。

每当我需要来自 SessionFactory 的新会话时,我都会执行以下操作:

protected Session getSession(SessionFactory sessionFactory)
   throws DataAccessResourceFailureException {
      Session session = org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(sessionFactory, true);
      session.setFlushMode(FlushMode.MANUAL);
      return session;
}

我继续获取会话然后使用它,如下所示:

SessionFactory sessionFactory = (SessionFactory) beanFactory.getBean(getSessionFactoryBeanName()); // beanFactory is org.springframework.beans.factory.BeanFactory injected
Session session = getSession(sessionFactory);
session.beginTx
Criteria creteria = session.createCriteria(MyBean.class);
creteria.add(Restrictions.eq("id",myBean.getBeanId() ));
List<MyBean> res = creteria.list();
// Do some work on the beans
session.commit() 

此时,res列表包含我期望的所有条目....但他们所有的子实体都是NULL.

以下是 Spring 应用程序上下文 XML 文件中的相关条目:

  <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="configLocation" value="hibernate.cfg.xml"></property>
  </bean>
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="get*" read-only="true"/>
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>
  <tx:advice id="txAdviceAll" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="*" />
    </tx:attributes>
  </tx:advice>
  <bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        scope="prototype" >
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="MyBeanDAO"
    class="com.ndipiazza.MyBeanDAO">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

我的应用程序上下文有缺陷吗?为什么我没有收到这些孩子的条目?我需要搜索一些特殊的条目吗?

这是 mybean.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.ndipiazza.MyBean" table="MY_BEAN" schema="ERESUSER_OWNER">
        <id name="myBeanId" type="java.lang.Long">
            <column name="MY_BEAN_ID" precision="22" scale="0" />
            <generator class="assigned" />
        </id>
        <many-to-one name="loadVerification" class="com.ndipiazza.LoadVerification" fetch="select">
            <column name="VERIFY_ID" precision="22" scale="0" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

这是 MyBean.java

package com.ndipiazza;

/**
 * MyBean entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class MyBean implements java.io.Serializable {

    // Fields

    private Long myBeanId;
    private LoadVerification loadVerification;

    // Constructors

    /** default constructor */
    public MyBean() {
    }

    /** minimal constructor */
    public MyBean(Long myBeanId, LoadVerification loadVerification) {
        this.myBeanId = myBeanId;
        this.loadVerification = loadVerification;
    }

    public Long getMyBeanId() {
        return this.myBeanId;
    }

    public void setMyBeanId(Long myBeanId) {
        this.myBeanId = myBeanId;
    }

    public LoadVerification getLoadVerification() {
        return this.loadVerification;
    }

    public void setLoadVerification(LoadVerification loadVerification) {
        this.loadVerification = loadVerification;
    }
}
4

0 回答 0