Hibernate在查询时不返回实体的子节点

时间:2012-08-12 19:31:04

标签: spring hibernate transactions

我遇到了与Hibernate + Spring Transaction Manager相关的问题,在从会话开始事务后查询hibernate对象时,我为所有Child实体获取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;
    }
}

0 个答案:

没有答案