是的,我見過類似的問題,但沒有一個答案真正讓我找到了解決方案。我沒有在任何地方使用線程,并且相同的代碼在另一個 jhipster 應用程序中工作,所以我很難過為什么這個提升+移位會導致休眠問題。public UserDetailsContextMapper userDetailsContextMapper() { return new UserDetailsContextMapper() { @Override public UserDetails mapUserFromContext( DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) { log.error("2 " + username + " -> " + ctx.toString()); String lowercaseLogonName = username.toLowerCase(); Optional<User> userFromDatabase = userRepository.findOneByLogin(lowercaseLogonName); if (!userFromDatabase.isPresent()) { User ldapUser = new User(); ldapUser.setLogin(lowercaseLogonName); ldapUser.setPassword(RandomStringUtils.random(60)); // We use LDAP password, but the password need to be set ldapUser.setActivated(true); try { Attribute attribute = ctx.getAttributes().get("mail"); ldapUser.setEmail( attribute != null && attribute.size() > 0 ? (String) attribute.get(0) : ""); attribute = ctx.getAttributes().get("givenname"); ldapUser.setFirstName(attribute != null && attribute.size() > 0 ? (String) attribute.get(0) : ""); attribute = ctx.getAttributes().get("sn"); ldapUser.setLastName(attribute != null && attribute.size() > 0 ? (String) attribute.get(0) : ""); } catch (NamingException e) { log.warn("Couldn't get LDAP details for user: " + lowercaseLogonName); } }編輯 - 這解決了我的問題:Optional<Authority> optAuthority = authorityRepository.findById(AuthoritiesConstants.USER);Authority authority = optAuthority.get();
1 回答

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
這一行:
Authority authority = authorityRepository.getOne(AuthoritiesConstants.USER);
只為您提供對實體的引用。它在后臺調用EntityManager#getReference
. 正如所說,這不是實體,只是對象關系映射目的的引用。拋出異常,因為您試圖將其轉換到事務環境之外。
有幾個解決方案。要獲取實體,您必須EntityManager#find
在事務中使用或執行它(使用@Transactional
方法)。在這里,您還可以調用Hibernate.initialize(authority)
以從數據庫中獲取實體。
添加回答
舉報
0/150
提交
取消