亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JPA LazyInitialisationException - 無法初始化代理

JPA LazyInitialisationException - 無法初始化代理

慕桂英546537 2022-06-15 15:13:58
是的,我見過類似的問題,但沒有一個答案真正讓我找到了解決方案。我沒有在任何地方使用線程,并且相同的代碼在另一個 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)以從數據庫中獲取實體。


查看完整回答
反對 回復 2022-06-15
  • 1 回答
  • 0 關注
  • 88 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號