@GetMapping("/teachers/{id}")
@Timed
public ResponseEntity<Teacher> getTeacher(@PathVariable Long id) {
log.debug("REST request to get Teacher : {}", id);
Teacher teacher = teacherRepository.findOne(SecurityUtils.getCurrentUserId());
teacher.getAddresses().size(); --------此处代码
return ResponseUtil.wrapOrNotFound(Optional.ofNullable(teacher));
}
当在OneToMany关系中手动触发延迟加载对象时出以下错误
2017-09-13 23:33:51.859 ERROR 5392 --- [ XNIO-3 task-1] com.zhile.aop.logging.LoggingAspect : Exception in com.zhile.web.rest.TeacherResource.getTeacher() with cause = 'NULL' and exception = 'failed to lazily initialize a collection of role: com.zhile.domain.Teacher.addresses, could not initialize proxy - no Session'
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.zhile.domain.Teacher.addresses, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:201)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143)
at com.zhile.web.rest.TeacherResource.getTeacher(TeacherResource.java:111)
at com.zhile.web.rest.TeacherResource$$FastClassBySpringCGLIB$$1bff0b37.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at com.zhile.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:48)
at com.ryantenney.metrics.spring.TimedMethodInterceptor.invoke(TimedMethodInterceptor.java:34)
这是由于没有配置在视图打开延迟加载特性所置,在SpringBoot的项目中通过以下配置设置:
jpa:
open-in-view: true --------此处配置
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: MYSQL
show-sql: true
properties:
hibernate.id.new_generator_mappings: true
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true