Home Articles

无法为事务打开Hibernate Session,JavaConfig

Asked
Viewed 1543 times
7

找不到错误((

Spring MVC Hibernate,JavaConfig

WebAppConfig:

package com.sprhib.init;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;


@Configuration
@ComponentScan("com.sprhib")
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class WebAppConfig {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        sessionFactoryBean.setHibernateProperties(hibProperties());
        return sessionFactoryBean;
    }

    private Properties hibProperties() {
        Properties properties = new Properties();
        properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));

        return properties;  
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

用户

package com.sprhib.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
@Entity
@Table(name="user")
public class User {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;
    private Integer age;
    private Boolean isAdmin;
    private Timestamp createdDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getAdmin() {
        return isAdmin;
    }

    public void setAdmin(Boolean admin) {
        isAdmin = admin;
    }

    public Timestamp getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Timestamp createdDate) {
        this.createdDate = createdDate;
    }
}



package com.sprhib.dao;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.sprhib.model.User;


@Repository
public class UserDAOImpl implements UserDAO {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {

        return sessionFactory.getCurrentSession();
    }



    public void addUser(User user) {

        getCurrentSession().save(user);
    }


    public void updateUser(User user) {
        User userUpdate = getUser(user.getId());
        userUpdate.setName(user.getName());
        userUpdate.setAge(user.getAge());
        userUpdate.setAdmin(user.getAdmin());
        userUpdate.setCreatedDate(user.getCreatedDate());
        getCurrentSession().update(userUpdate);
    }


    public User getUser(int id) {

        return (User)getCurrentSession().get(User.class,id);
    }


    public void deleteUser(int id) {
        User user = getUser(id);
        if (user!=null)
            getCurrentSession().delete(user);

    }


    public List<User> getUsers() {
        System.out.println("zzz");
        return getCurrentSession().createQuery("FROM User").list();
    }
}

在UserDAOImpl:

package com.sprhib.dao;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.sprhib.model.User;


@Repository
public class UserDAOImpl implements UserDAO {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {

        return sessionFactory.getCurrentSession();
    }



    public void addUser(User user) {

        getCurrentSession().save(user);
    }


    public void updateUser(User user) {
        User userUpdate = getUser(user.getId());
        userUpdate.setName(user.getName());
        userUpdate.setAge(user.getAge());
        userUpdate.setAdmin(user.getAdmin());
        userUpdate.setCreatedDate(user.getCreatedDate());
        getCurrentSession().update(userUpdate);
    }


    public User getUser(int id) {

        return (User)getCurrentSession().get(User.class,id);
    }


    public void deleteUser(int id) {
        User user = getUser(id);
        if (user!=null)
            getCurrentSession().delete(user);

    }


    public List<User> getUsers() {

        return getCurrentSession().createQuery("FROM User").list();
    }
}

UserController的:

package com.sprhib.controller;
import java.util.List;

import com.sprhib.model.User;
import com.sprhib.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/user")
public class UserController  {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/add", method=RequestMethod.GET)
    public ModelAndView addUserPage()
    {
        ModelAndView modelAndView = new ModelAndView("add-user-form");
        modelAndView.addObject("user",new User());
        return modelAndView;
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    public ModelAndView addingUser(@ModelAttribute User user) {

        ModelAndView modelAndView = new ModelAndView("home");
        userService.addUser(user);
        String message = "User was successfully added.";
        modelAndView.addObject("message", message);

        return modelAndView;
    }


    @RequestMapping(value="/list")
    public ModelAndView listOfUsers() {
        ModelAndView modelAndView = new ModelAndView("list-of-users");
        List<User> users = userService.getUsers();
        modelAndView.addObject("users", users);
        return modelAndView;
    }

    @RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
    public ModelAndView editUserPage(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("edit-user-form");
        User user = userService.getUser(id);
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
    public ModelAndView edditingUser(@ModelAttribute User user, @PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("home");
        userService.updateUser(user);
        String message = "User was successfully updated.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
    public ModelAndView deleteUser(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("home");
        userService.deleteUser(id);
        String message = "User was successfully deleted.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

}

application.properties:

#DB properties:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=root

#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
entitymanager.packages.to.scan=com.sprhib.model

当我尝试在浏览器中获取用户时:

HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / engine / transaction / spi / TransactionContext类型异常报告消息请求处理失败;嵌套异常是org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / engine / transaction / spi / TransactionContext description服务器遇到内部错误,导致无法完成此请求 . exception org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / engine / transaction / spi / TransactionContext org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:977)org.springframework.web.servlet.FrameworkServlet.doGet( FrameworkServlet.java:856)javax.servlet.http.HttpServlet.service(httpServlet.java:622)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)javax.servlet.http.HttpServlet.service( HttpServlet.java:729)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)根本原因org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / engine / transaction / spi / TransactionContext org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:544)org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction( AbstractPlatformTransactionManager.java:373)org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:463)org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)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 . 代理 . $ Proxy34.getUsers(未知来源)com.sprhib.controller.UserController.l istOfUsers(UserController.java:44)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang .reflect.Method.invoke(Method.java:497)org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)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.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775 )org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)org.springframework.web.servlet.mvc.method.AbstractHandle rMethodAdapter.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:965)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)javax.servlet.http.HttpServlet.service(httpServlet.java:622)org . springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)javax.servlet.http.HttpServlet.service(HttpServlet.java:729)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)root cause java.lang.NoClassDefFoundError:org / hibernate / engine / transaction / spi / TransactionContext org.springframework.orm.hibernate4.HibernateTransactionManager.isSameConnectionForEntireSession(HibernateTransactionManager.java:711)org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:445)org.springframework.transaction.support.AbstractPlatformTransactionManager .getTransaction(AbstractPlatformTransactionManager.java:373)org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:463)org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)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 . $ Proxy34.getUsers(Unknown Source)com.sprhib.controller . UserController.listOfUsers(UserController.java:44)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java .lang.reflect.Method.invoke(Method.java:497)org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)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.RequestMappingHandlerAdap ter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)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:965)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)javax.servlet.http.HttpServlet.service(httpServlet.java:622)org.springframework.web . servlet.FrameworkServlet.service(FrameworkServlet.java:841)javax.servlet.http.HttpServlet.service(HttpServlet.java:729)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)根本原因java.lang.ClassNotFoundException:org.hiberna te.engine.transaction.spi.TransactionContext org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)org.springframework.orm .hibernate4.HibernateTransactionManager.isSameConnectionForEntireSession(HibernateTransactionManager.java:711)org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:445)org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)org .springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:463)org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java :96)org.springframework.aop.fr amework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)com.sun.proxy . $ Proxy34.getUsers(Unknown Source)com.sprhib.controller . UserController.listOfUsers(UserController.java:44)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:497)org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) 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.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)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:965)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)javax . servlet.http.HttpServlet.service(HttpServlet.java:622)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)javax.servlet.http.HttpServlet.service(httpServlet.java:729)org . apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我正在训练this例子......

1 Answer

  • 22

    你使用 org.springframework.orm.hibernate4.HibernateTransactionManager 用于Hibernate 4.这个类使用来自Hibernate 4的 TransactionContext .

    看起来你使用Hibernate 5.只需改变

    import org.springframework.orm.hibernate4.HibernateTransactionManager;
    import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
    

    对此

    import org.springframework.orm.hibernate5.HibernateTransactionManager;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
    

    当然,您需要使用相应的Spring库 . 例如4.2.4.RELEASE .

Related