Hallo all,
i got a problem by using CrudRepository. Example: i have two entities, entity A has a collection of entity B.
then i got 2 repositories.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 }
but when i got this, i got a org.hibernate.LazyInitializationException, how can i avoid this?Code:ARepository extends CrudRepository<A, int>{} BRepository extends CrudRepository<B, int>{}
a.bs(the collection) would not be loaded, and always throw out a org.hibernate.LazyInitializationExceptionCode:@Service @Transactional(readOnly=true) class ServiceImpl implements Service { @Resource ARepository ar; @Override A a = ar.findOne(int id); }
Thank you in advance!!
EDIT:
if i load a collection of a object lazily, then how can i iniate properly, like:
here is the applicationContext.xml: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 web.xmlCode:<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" />
Thanks..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>


Reply With Quote