gpt4 book ai didi

java - Spring 事务内的 Hibernate 实体生命周期和 session 生命周期

转载 作者:太空宇宙 更新时间:2023-11-04 14:29:42 27 4
gpt4 key购买 nike

我在理解 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>();

和聚合实体

@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 被调用,代理看到 Activity 事务,并获取现有资源3. 数据检索4. 交易已结束...

但这并没有发生,每个方法调用中似乎有两个不同的事务,并且实体在调用之间分离,发出惰性初始化异常。

实际输出日志是这样的:

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:我认为 View 过滤选项比重新附加选项更糟糕,它可能会在高并发下导致严重的性能问题。

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

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>

最佳答案

测试类也应该是事务性的

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

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

关于java - Spring 事务内的 Hibernate 实体生命周期和 session 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26282012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com