gpt4 book ai didi

java - Spring MVC Hibernate session 错误

转载 作者:行者123 更新时间:2023-12-01 21:48:23 25 4
gpt4 key购买 nike

我正在尝试将 Hibernate 与 spring 结合使用并执行用户注册操作。这是代码

@Repository
@Transactional(value = "myTransactionManager")
public class UserFunctionsDAOImpl implements UserFunctionsDAO {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}

@Override
public String registerUser(Users user) {
// TODO Auto-generated method stub

Query query = this.sessionFactory.getCurrentSession().createQuery("SELECT COUNT(*) FROM Users WHERE emailID = :email_ID OR mobileNo = :mobile_No");
query.setString("email_ID", user.getEmailID());
query.setString("mobile_No", user.getMobileNo());

if(((long)query.uniqueResult()) > 0)
return "Email or Mobile number already exists";
else
{
user.setUsetType("u");
Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();

return "User Registered Successfully";
}
}

}

在上面的代码中,我收到错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.TransactionException: nested transactions not supported
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.hibernate.TransactionException: nested transactions not supported
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1471)
com.pir.dao.UserFunctionsDAOImpl.registerUser(UserFunctionsDAOImpl.java:49)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy620.registerUser(Unknown Source)
com.pir.service.UserFunctionsServiceImpl.registerUser(UserFunctionsServiceImpl.java:36)
com.pir.controller.RegisterController.addUserController(RegisterController.java:63)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

如何消除这个错误?

最佳答案

这里的@Transactional注释有问题。 Spring 将代理您的 @Repository Bean,将所有公共(public)函数包装在启动新事务的代码中。

因此,调用该方法将在 Spring 代理内创建一个事务,然后您尝试使用自己的代码启动另一个事务。

要解决这个问题,您可以不自己启动另一个事务,而只使用 Spring 提供的事务,或者您可以将 @Transactional 移至您实际需要的方法,而不是每个 public类中的方法。

关于java - Spring MVC Hibernate session 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600715/

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