LazyInitializationException,活动事务和连接打开

Cam*_*ego 1 java spring hibernate jpa-2.0 jboss7.x

我知道这已被问过很多次了(已经在这里和其他网站上发布了大部分帖子),但我没有解决我的问题.

我的设置是:jpa 2 + hibernate 4 + spring 4 + primefaces + jboss eap 7

问题:我有一个懒惰的集合到另一个bean,但是当我在bean上调用.size()方法时,它会抛出"LazyInitializationException:懒得初始化一个角色集合:com.pe.controlLines.data .model.Activity.activityRisks,无法初始化代理 - 没有会话"

我确定在@Transactional方法和 http://blog.timmattison.com/archives/2012/04/19/tips-for-debugging-springs-transactional-annotation/之后,这个LazyInitializationException之后有一个活动的事务所以我是100%确定当时正在运行的交易.

我的实体声称:

@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;
Run Code Online (Sandbox Code Playgroud)

和嵌套类

@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;
Run Code Online (Sandbox Code Playgroud)

bussines委托是这样的anotated(从页面控制器调用bussines):

@Component
@Scope("session")
@Transactional
public class SystemConfigurationBussinesDelegate {
Run Code Online (Sandbox Code Playgroud)

得到了对实体的引用的实际化,执行正常.(这是来自aboce班)

private Company currentCompany;

    private Risk currentRisk;

    @PostConstruct
    public void init(){
        //((WordDAO)wordDAO).startIndexer();
        currentCompany = genericDAO.get(Company.class, 1l);
    }
Run Code Online (Sandbox Code Playgroud)

但是在这种方法中

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;
    }
Run Code Online (Sandbox Code Playgroud)

sysout返回true,因此事务处于活动状态,我可以看到与数据库的开放连接,Hibernate.inicialize工作正常,以及对currentCompany.getCompanieActivities()的调用.getActivityRisks().size(); 抛出异常.

它可能是上下文或某些类似的问题吗?

我的弹簧配置:

<?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>
Run Code Online (Sandbox Code Playgroud)

ger*_*tan 5

我认为你的问题是在内部调用事务方法@PostConstruct.通过设计,弹簧tx方面在postconstruct方法上/可能不活动,因为并非所有bean都保证已经完成构建.尝试搜索这个主题,我记得也遇到过这个惊喜,但有很多有用的文章.

从那以后,当我在postconstruct上需要tx时,我首选的替代方法是使用编程事务(参见Spring TransactionTemplate模式)使用ContextRefreshedEvent.见下面的例子:

@Service
public class MyService implements ApplicationListener<ContextRefreshedEvent> {

  public void onApplicationEvent(ContextRefreshedEvent event) {
    // This method will be executed at context startup or refresh
    // It is guaranteed all beans have finish constructing, hence
    // AOP tx is available
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)