close
使用 Hibernate Tool 產生 Model 時, FetchType 預設是 FetchType.LAZY , 但在執行時, 卻常會產生類似下列 no Session 的錯誤訊息
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.jwt.db.maria.model.authentication.User.userAuthorities, could not initialize proxy - no Session
這時候需要在 Method 前加入 @Transactional 就可以解決
@Test
@Transactional
public void testUserAuthority() {
User user = userRepository.findById(1L).get();
JwtUser jwtUser = JwtUserFactory.create(user);
logger.info(jwtUser.getUsername());
}
Ref: http://javasampleapproach.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql
全站熱搜
留言列表