gpt4 book ai didi

java - org.hibernate.LazyInitializationException : even with @Transactional

转载 作者:行者123 更新时间:2023-12-01 14:17:40 26 4
gpt4 key购买 nike

我正在尝试打印来自一个用户的所有帖子,但无法加载一组帖子,并且出现此异常(下面的 Stacktrace)。

Controller

@RequestMapping(value = "/mainPage", method = RequestMethod.GET)
public ModelAndView getMainPage(Authentication authentication, /*@ModelAttribute("post") Post post, */ModelMap modelMap)
{
ModelAndView modelAndView = new ModelAndView("mainPage", "command", new Post());
modelAndView.addObject(ERROR_ATTRIBUTE, modelMap.get(ERROR_ATTRIBUTE));

//TODO what if don't find?
//User user = userManager.findByUsername(authentication.getName());
//modelAndView.addObject("user", user);
//modelAndView.addObject("posts", userManager.getUsersPosts(user.getUsername()));
// //modelAndView.addObject("user", user);

modelAndView.addObject("posts", userManager.getUsersPosts(authentication.getName()));
return modelAndView;

//return new ModelAndView("mainPage", "command", new Post());
}

用户管理器
@Service
@Transactional
public class DefaultUserManager implements UserManager{

@Override
public User findByUsername(String username) {
return userDao.findByUsername(username);
}

@Override
public Set<Post> getUsersPosts(String username) {
User user = findByUsername(username);
return user.getPosts();
}
@Override
public List<Post> getUsersPosts(String username) {
return userDao.findPostsByUsername(username);
}
}

用户
@Entity
@Table(name = "Users")
public class User extends BaseObject implements UserDetails {

/** User's posts */
private Set<Post> posts;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
//@BatchSize(size = 5) /* Na základě odhadu, že průměrný uživatel bude mít max 5-10 šablon */
//@OrderBy(" DESC")
@JoinColumn(name="createdBy_ID")
public Set<Post> getPosts() {
return posts;
}
}

我正在通过服务层访问帖子 @Transactional类,所以我不确定到底出了什么问题。起初我在注释中使用了代码,所以我认为这是因为我收到了 userController第一个和以后的帖子但更改为在一行中调用它没有帮助。你能给我提示如何解决这个问题吗?
我想避免使用 FetchType.EAGER .
Stacktrace
root cause

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: sk1x1.domain.User.posts, could not initialize proxy - no Session
org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
org.apache.jsp.WEB_002dINF.pages.mainPage_jsp._jspx_meth_c_005fforEach_005f0(mainPage_jsp.java:451)
org.apache.jsp.WEB_002dINF.pages.mainPage_jsp._jspService(mainPage_jsp.java:157)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)

编辑。

我尝试使用 Kayaman 方法,所以编辑了我的代码,现在我得到了这个:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=sk1x1.domain.User.posts,tableName=Posts,tableAlias=posts1_,origin=Users user0_,columns={user0_.id ,className=sk1x1.domain.Post}}]

这是我的 UserDaoJpa更改后
public List<Post> findPostsByUsername(String username)
{
User user = findByUsername(username);

if(user == null) {
throw new UsernameNotFoundException(username + " was not found.");
}

TypedQuery<Post> query = em.createQuery("select u.posts from User u left join fetch u.posts where u.username = :username", Post.class);
query.setParameter("username", username);
try {
return query.getResultList();
}
catch (NoResultException e)
{
return null;
}
}

你有什么建议吗?

最佳答案

当您return user.getPosts();您正在返回未初始化的惰性代理。当它在 JSP 中被访问时,事务上下文早已不复存在,并且在此之前您不希望使用 Open Session In View 反模式来保持事务上下文打开。

我会写一个单独的查询来获取帖子并使用 JOIN FETCH急切地得到他们,而不会使关系急切。

如下所示,有两个选项取决于您是否想要 User过与否。

public Set<Post> getUsersPosts(String username) {
return userDao.findPostsByUsername(username);
}

// Only select posts from a user, without user entity. No JOIN needed.
@Query("SELECT u.posts FROM User u WHERE u.username = :username")
Set<Post> findPostsByUsername(@Param("username") String username);

// A Left join brings us the user even if it doesn't have posts, and
// FETCH gets the posts eagerly, so no lazy loading or performance hit
@Query("SELECT u FROM User u LEFT JOIN FETCH u.posts WHERE u.username = :username")
User findUserAndPostsByUsername(@Param("username") String username);

关于java - org.hibernate.LazyInitializationException : even with @Transactional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48196125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com