Hi,
Can anybody please help me for this famous LazyInitializationException problem?
The exception is thrown when I call "type.getMetadata().getHasChild()" in MetadataService.test method.
I searched this issue on web but no clues found hit my problem, the typical suggestions such as:
-openSessionInView: I configured and actually lazy loading in JSP works fine.
-use transaction: actually @Transactional has alreay configured on method, but I don't know if it take effect.
Controller:
Service:Code:@Controller @RequestMapping("/system") public class SystemController { @Autowired private MetadataRepository metaRepo; @Autowired private TypeRepository typeRepo; @Autowired TypeService typeServ; @Resource MetadataService metaServ; @RequestMapping(value = "/metadata/traverse/{id}", method = RequestMethod.GET) public @ResponseBody String getFullMeta(@PathVariable Long id) { StringBuilder json=new StringBuilder(); metaServ.test(); return json.toString(); }
Domain ClassCode:@Service public class MetadataService { @Autowired private MetadataRepository metaRepo; @Autowired private TypeRepository typeRepo; @Transactional public void test(){ StringBuilder json=new StringBuilder();; List<Type> types=typeRepo.findByParentId(2140L); for(Type type: types){ if(type.getMetadata()!=null) json.append('{'); json.append('t'); json.append(type.getId()); if(type.getMetadata()!=null){ if(type.getMetadata().getHasChild()){ //Exception here!!!!!!!!!!!!! json.append(type.getMetadata().getHasChild()); }else{ json.append(type.getMetadata().getId()); } json.append('}'); } json.append(','); //TODO type instantiate } } }
applicationContext.xmlCode:@Entity public class Type extends BaseEntity implements Serializable { private static final long serialVersionUID = 1L; @Column(length = 40, nullable = false) private String name; @ManyToOne(fetch = FetchType.LAZY) private Type type; @ManyToOne(fetch = FetchType.LAZY) private Type parent; private Boolean hasChild; @OneToMany(mappedBy = "parent" ) @OrderBy(value = "id") private Set<Type> children; @ManyToOne(fetch = FetchType.LAZY) private Metadata metadata; //the way convert type(string) to Object(MetaData) private String description; }
app-servlet.xmlCode:<!-- define EntiryManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="MysqlUnit" /> <property name="dataSource" ref="dataSource" /> <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="MYSQL" /> </bean> </property> </bean> <!-- configure individual data source to be shared between emf and jdbc --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/db" /> <property name="user" value="root" /> <property name="password" value="password" /> </bean> <!-- Post-processor to perform exception translation on @Repository classes (from native exceptions such as JPA PersistenceExceptions to Spring's DataAccessException hierarchy). --> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!--定义JPA的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <context:annotation-config /> <tx:annotation-driven />
Log inforamtionCode:<mvc:annotation-driven /> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> <bean id="openSessionInViewInterceptor" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </mvc:interceptor> </mvc:interceptors> <!-- The controllers are auto-detected POJOs labeled with the @Controller annotation. --> <context:component-scan base-package="com.azelea.*.controllers" /> <!-- The controllers are auto-detected POJOs labeled with the @Service annotation. --> <context:component-scan base-package="com.azelea.*.services" />
Code:2012-08-19 11:04:36,595 DEBUG: Adding transactional method 'test' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' >>> org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:106) 2012-08-19 11:04:36,725 DEBUG: Skipping transactional joinpoint [com.azelea.SystemMgmt.services.MetadataService.test] because no transaction manager has been configured >>> org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:339) 2012-08-19 11:04:47,580 DEBUG: Creating new EntityManager for shared EntityManager invocation >>> org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:231) Hibernate: select type0_.id as id2_, type0_.description as descript2_2_, type0_.hasChild as hasChild2_, type0_.metadata_id as metadata5_2_, type0_.name as name2_, type0_.parent_id as parent6_2_, type0_.type_id as type7_2_ from Type type0_ where type0_.parent_id=? 2012-08-19 11:04:47,916 DEBUG: User-defined constructor called on DocumentBacked object of class class com.azelea.SystemMgmt.entities.Type >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.ajc$before$org_springframework_data_mongodb_crossstore_MongoDocumentBacking$2$adf21a1(MongoDocumentBacking.aj:142) ... 2012-08-19 11:04:47,958 DEBUG: JPA lifecycle event PostLoad: com.azelea.SystemMgmt.entities.Type :: 2141 >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.ajc$interMethod$org_springframework_data_mongodb_crossstore_MongoDocumentBacking$org_springframework_data_mongodb_crossstore_DocumentBacked$itdPostLoad(MongoDocumentBacking.aj:229) 2012-08-19 11:04:47,961 DEBUG: Transaction synchronization is not active for 2141 >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.registerTransactionSynchronization(MongoDocumentBacking.aj:168) 2012-08-19 11:04:47,965 DEBUG: User-defined constructor called on DocumentBacked object of class class com.azelea.SystemMgmt.entities.Metadata_$$_javassist_7 >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.ajc$before$org_springframework_data_mongodb_crossstore_MongoDocumentBacking$2$adf21a1(MongoDocumentBacking.aj:142) 2012-08-19 11:04:47,974 DEBUG: JPA lifecycle event PostLoad: com.azelea.SystemMgmt.entities.Type :: 2142 >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.ajc$interMethod$org_springframework_data_mongodb_crossstore_MongoDocumentBacking$org_springframework_data_mongodb_crossstore_DocumentBacked$itdPostLoad(MongoDocumentBacking.aj:229) 2012-08-19 11:04:47,992 DEBUG: Transaction synchronization is not active for 2142 >>> org.springframework.data.mongodb.crossstore.MongoDocumentBacking.registerTransactionSynchronization(MongoDocumentBacking.aj:168) ... 2012-08-19 11:04:48,008 DEBUG: Closing JPA EntityManager >>> org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager(EntityManagerFactoryUtils.java:343)


Reply With Quote
