Results 1 to 2 of 2

Thread: HELP!org.hibernate.LazyInitializationException( by using CrudRepository)

  1. #1
    Join Date
    Jun 2012
    Posts
    5

    Default HELP!org.hibernate.LazyInitializationException( by using CrudRepository)

    Hallo all,

    i got a problem by using CrudRepository. Example: i have two entities, entity A has a collection of entity B.
    Code:
    class A {
      int id;
      int name;
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      Set<B> bs;
      // getters and setters
    }
    class B {
      int id;
      int name;
      @ManyToOne(mappedBy="bs")
      A a;
      // getters and setters
    }
    then i got 2 repositories.
    Code:
    ARepository extends CrudRepository<A, int>{}
    BRepository extends CrudRepository<B, int>{}
    but when i got this, i got a org.hibernate.LazyInitializationException, how can i avoid this?
    Code:
    @Service
    @Transactional(readOnly=true)
    class ServiceImpl implements Service {
    @Resource ARepository ar;
    
    @Override
    A a = ar.findOne(int id);
    }
    a.bs(the collection) would not be loaded, and always throw out a org.hibernate.LazyInitializationException

    Thank you in advance!!

    EDIT:

    if i load a collection of a object lazily, then how can i iniate properly, like:
    Code:
    A a = ar.findOne(int id);
    // a.bs is not loaded;
    // then when i wanna to access a.bs, what should i do?
    here is the applicationContext.xml:
    Code:
    <jpa:repositories base-package="com.myproject.repository" />
    
    <context:component-scan base-package="com.myproject.*" />
    <context:annotation-config />
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="keep-apm" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="database" value="POSTGRESQL"/>
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
            </bean>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://127.0.0.1:5432/db" />
    </bean>
    <bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory">
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    here is the web.xml
    Code:
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.myproject.util.LogLocator</listener-class>
    </listener>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    Thanks..
    Last edited by jaiwo99; Jul 2nd, 2012 at 09:21 AM.

  2. #2

    Default

    You state in your mapping of entity A that the collection bs should be loaded lazily. If you want to load that collection every time when the entity A is fetched, you should change your mapping to:

    Code:
    class A {
      int id;
      int name;
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      Set<B> bs;
      // getters and setters
    }
    Last edited by Loke; Jul 16th, 2012 at 05:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •