我知道這已被問了很多次(已經在這裏和其他網站大多數帖子cheched),但我沒有得到解決我的問題。LazyInitializationException,活動的事務和連接打開
我的設置是:JPA 2 + hibernate的4個+彈簧4 + primefaces +的JBoss EAP 7
的問題:我有一個延遲集合到另一個bean,但是當我打電話給.size()方法在bean上,它拋出「LazyInitializationException:無法懶惰地初始化一個角色集合:com.pe.controlLines.data.model.Activity.activityRisks,無法初始化代理 - 沒有會話」
我確定我有一個在這個LazyInitializationException Within a @Transactional Method和http://blog.timmattison.com/archives/2012/04/19/tips-for-debugging-springs-transactional-annotation/之後的活動交易,所以我100%確定當時正在運行的交易。
我的實體clases:
@Entity
public class Company {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long companyId;
@Column
private String name;
@OneToOne(cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
private Activity companieActivities;
@OneToMany
private Collection<SourceSupervision> sourceSupervisions;
和嵌套類
@Entity
@Indexed
public class Activity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long activityId;
@ManyToOne
@JoinColumn(name="parentActivityId")
private Activity parent;
@Column
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "searchtokenanalyzer")
private String name;
@Column
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "searchtokenanalyzer")
private String description;
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
private Collection<ActivityRisk> activityRisks = new ArrayList<ActivityRisk>();
@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
private Collection<Word> words;
@ManyToMany
private Collection<Rol> rolesForActivity;
的bussines委託anotated像這樣(的bussines從頁面控制器調用):
@Component
@Scope("session")
@Transactional
public class SystemConfigurationBussinesDelegate {
獲得對該實體的引用的官方化,該實體運行良好。 (這是從aboce類)
private Company currentCompany;
private Risk currentRisk;
@PostConstruct
public void init(){
//((WordDAO)wordDAO).startIndexer();
currentCompany = genericDAO.get(Company.class, 1l);
}
但在此方法,以便該交易活躍
public List<Danger> getDangers(){
List<Danger> returnValue = new ArrayList<Danger>();
System.out.println(TransactionSynchronizationManager.isActualTransactionActive());
Hibernate.initialize(currentCompany.getCompanieActivities());
currentCompany.getCompanieActivities().getActivityRisks().size();
for(ActivityRisk aRisk : currentCompany.getCompanieActivities().getActivityRisks()){
Risk risk = aRisk.getRisk();
if(risk == currentRisk){
returnValue = new ArrayList<Danger>(aRisk.getDangers());
}
}
return returnValue;
}
的系統輸出返回true,我可以看到一個打開的連接到數據庫,Hibernate的。 inicialize正常工作,並調用currentCompany.getCompanieActivities()。getActivityRisks()。size();拋出異常。
它可能是上下文或somethng類似的問題嗎?
我的Spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<context:component-scan base-package="com.pe.controlLines" />
<context:annotation-config />
<context:spring-configured />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<jee:jndi-lookup id="myDataSource" jndi-name="java:/ControllinesDS"/>
<!-- Data Source Declaration -->
<!-- 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>net.javabeat.spring.model</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>co.com.testTalos.model.Storage</value>
<value>co.com.testTalos.model.Buyer</value>
<value>co.com.testTalos.model.Preferences</value>
</list>
</property>-->
<property name="packagesToScan">
<list>
<value>com.pe.controlLines.data.model</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>
</beans>
你甚至不能想象你是多麼高興,你只是讓我......謝謝你,你的答案和一些JUnit測試我得解決我所有的問題。 – 2014-10-07 03:16:49