【发布时间】:2017-05-31 06:37:15
【问题描述】:
我的 Spring Boot 应用程序中存在延迟初始化问题。我有一个带有惰性字段Role 的实体,我在我的Spring Security (UserDetailsService) 方法中有LazyInitializationException,但在控制器中没关系。
你能向我解释一下 Spring Boot 如何与 fetch = FetchType.LAZY 一起工作吗?为什么它不能在 Spring Security UserDetailsService 和控制器方法中工作?
我没有找到任何关于此的指南。谢谢!
春季启动:
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
一个实体:
@Entity
@Table(name = "users")
@Getter
@Setter
@NoArgsConstructor
public class Users {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "Users_Role", joinColumns = @JoinColumn(name = "User_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
private Set<Role> roles = new HashSet<Role>();
}
我的服务:
@Transactional(readOnly = true)//does not matter
public Users getUserByLogin(String login) {
return usersRepository.findOneByLogin(login);
}
@Transactional(readOnly = true)
public Users getUserByLoginWithRoles(String login) {
Users oneByLogin = usersRepository.findOneByLogin(login);
logger.debug("User was initialize with Roles: " + oneByLogin.getRoles().size()); // force initialize of roles and it works!
return oneByLogin;
}
@Transactional(readOnly = true)//does not matter
public Users testGetUser() {
Users oneByLogin = usersRepository.getOne(1L);
return oneByLogin;
}
@Transactional(readOnly = true)//does not matter
public Users testFindUser() {
Users oneByLogin = usersRepository.findOne(1L);
return oneByLogin;
}
我有 Spring Security UserDetailsService:
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private Services services;
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
Users user;
Users userFetchedViaGet = services.testGetUser();
Users userFetchedViaCustomMethod = services.getUserByLogin(login);
Users userFetchedViaFind = services.testFindUser();
Users userFetchedWithRoles = services.getUserByLoginWithRoles(login);
try {
userFetchedViaGet.getRoles().add(new Role("test"));
} catch (Exception e) {
e.printStackTrace();//LazyInitializationException: failed to lazily initialize a collection of role: , could not initialize proxy - no Session
}
try {
userFetchedViaCustomMethod.getRoles().add(new Role("test"));
} catch (Exception e) {
e.printStackTrace();//LazyInitializationException: failed to lazily initialize a collection of role: , could not initialize proxy - no Session
}
try {
userFetchedViaFind.getRoles().add(new Role("test")); //LazyInitializationException: failed to lazily initialize a collection of role: , could not initialize proxy - no Session
} catch (Exception e) {
e.printStackTrace();
}
//some code
}
}
和我的控制器(所有方法都有效!但是必须发生异常,因为没有会话和延迟获取类型):
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
Users userFetchedViaGet = services.testGetUser();
Users userFetchedViaCustomMethod = services.getUserByLogin("ADMIN");
Users userFetchedViaFind = services.testFindUser();
Users userFetchedWithRoles = services.getUserByLoginWithRoles("ADMIN");
try {
userFetchedViaGet.getRoles().add(new Role("test"));
} catch (Exception e) {
e.printStackTrace();
}
try {
userFetchedViaCustomMethod.getRoles().add(new Role("test"));
} catch (Exception e) {
e.printStackTrace();
}
try {
userFetchedViaFind.getRoles().add(new Role("test"));
} catch (Exception e) {
e.printStackTrace();
}
//some code
}
【问题讨论】:
-
您好!那篇文章与我的问题没有任何共同之处。
-
你能解决这个问题吗?我其实也有同样的问题
标签: java hibernate spring-mvc spring-boot spring-security