Spring事务中的Hibernate实体生命周期和会话生命周期

时间:2014-10-09 15:14:21

标签: java spring hibernate jpa-2.0

我在理解spring处理hibernate实体的方式时遇到了一些麻烦,并且进行了延迟加载过程。

因此,对于这种情况,我们必须使用实体

@Entity
public class EntityA{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;


    @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
    private Collection<EntityB> bss= new ArrayList<EntityB>();

和agregated entity

@Entity
public class EntityB{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;


    @ManyToMany(mappedBy="bss")
    private Collection<EntityA> ass= new ArrayList<EntityA>();

然后我有一个标记为事务性的简单业务类:

@Component
@Scope("session")
@Transactional(propagation=Propagation.TRIED_EVERY_SINGLE_ONE)
public class GenericTestBean {

    private EntityA entityA;

    @Autowired
    private IGenericDAO genericDAO;


    public GenericTestBean() {
        System.out.println("bean creado!");
    }

    public void testQuery() {
        entityA= genericDAO.get(EntityA.class, 1l);
        System.out.println(TransactionIndicatingUtil.getTransactionStatus(true));
        System.out.println("is element atached? :" + genericDAO.isAtached(entityA));
        //this.loadData();

    }

    public void loadData(){

        System.out.println(TransactionIndicatingUtil.getTransactionStatus(true));
        System.out.println("is element atached? :" + genericDAO.isAtached(currentCompany));

        System.out.println(entityA.getBss.size());
    }

}

和一个简单的测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "applicationContext.xml" })
@WebAppConfiguration
public class EntityQueryTest {

//  @Autowired
//  private SessionFactory sessionFactory;

    @Autowired
    GenericTestBean genericTestBean;


    @Test
    public void consultarCompania(){

        genericTestBean.testQuery();
        genericTestBean.loadData();
    }

我认为应该发生的是: 1. GenericTestBean被实例化 2.从开始事务的代理外部调用testQuery 2.调用loadData,代理看到活动事务,并获取现有资源 3.检索数据 4.交易已经结束......

但是这没有发生,每个方法调用中似乎有两个不同的事务,实体在调用之间分离,发出一个懒惰的init异常。

实际输出日志是这样的:

bean creado!  --here the object get created
Hibernate: select company0_.companyId as ..... --here the first query get executed
[com.pe.controlLines.data.dao.GenericTestBean.testQuery] --here we check the name for the transaction
is element atached? :true --and see if the element is attached to the session
[com.pe.controlLines.data.dao.GenericTestBean.loadData]  --this is in the second method call (with a different transaction name :O )
is element atached? :false --both now, the same element get detached

我尝试重新附加实体,但是这给了我一个新的数据库查询(发送表EntityA的新查询以及获取对象的查询,我真的不喜欢)。

我希望保存一个额外的查询只是为了延迟加载,或者它必须是这样吗?或者我可能有一些配置错误?

Pdta:我认为视图过滤器选项甚至比重新附加选项更差,它可能导致高并发性下的严重性能问题。

任何人都可以澄清方法调用之间的事务上下文的行为,以及它与会话和实体状态的关系吗?

TransactionIndicatingUtil实现取自http://java.dzone.com/articles/monitoring-declarative-transac?page=0,1

并且通用dao是在这个想法之后构建的 General or specific DAO to record delivery with information from multiple tables?

更新

如果有一些用处,这里是spring配置文件

<context:component-scan base-package="xxxxxx" />

    <context:annotation-config />
    <context:spring-configured />

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- Data Source Declaration -->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://localhost:3306/xxxx" />

        <property name="username" value="xxxxx" />
        <property name="password" value="xxxxx" />
        <property name="initialSize" value="2" />
        <property name="minIdle" value="0" />
        <property name="minEvictableIdleTimeMillis" value="120000" />
        <property name="maxActive" value="20" />
        <property name="maxWait" value="5000" />
    </bean>

    <!-- Session Factory Declaration <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> -->
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.xx.xx.xx.xx</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.search.default.directory_provider">filesystem</prop>
                <prop key="hibernate.search.default.indexBase">C:/DEVELOPMENT/lucene/indexes</prop>


            </props>
        </property>
    </bean>

    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

    <!-- Transaction Manager is defined -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

2 个答案:

答案 0 :(得分:0)

JUnit测试本身不是事务性的。 GenericTestBean bean的每个方法都是事务性的。因此,每次非事务性测试调用事务bean的方法时,都会启动事务,执行bean方法,并提交事务。由于您连续调用两个事务方法,因此启动了两个单独的事务。

如果测试方法本身是事务性的,那么将为测试方法启动一个事务,并且(默认情况下),两个bean方法将在现有事务的上下文中执行,由测试启动。在测试方法返回后,事务将提交。

答案 1 :(得分:0)

Test类也应该是事务性的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "applicationContext.xml" })
@Transactional // <--- add
public class EntityQueryTest {

我假设您的applicationContext.xml文件是帖子中显示的XML代码

相关问题