1. 程式人生 > >Spring-data-jpa 整合Hibernate時延遲載入問題

Spring-data-jpa 整合Hibernate時延遲載入問題

    @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