Java – spring MVC and Hibernate cannot obtain the session of transaction synchronization for the current thread
•
Java
I have a spring application that works with hibernate Hibernate session is not created Throw an error as shown below
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.oi.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- data source --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.MysqL.jdbc.Driver" /> <property name="url" value="jdbc:MysqL://localhost:61391/springmvc2k" /> <property name="username" value="adminHYMgZHE" /> <property name="password" value="byIkcunaje5K" /> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionfactorybean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MysqLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="packagesToScan" value="com.oi.bean"></property> </bean> <!-- The transaction manager --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
My beans
import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Entity; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @Entity @Table(name="users") public class User { @Id @GeneratedValue int id; @Column(name = "username") String name; @Column(name = "password") String password; @Column(name = "email") String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getpassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
My spring controller is as follows
import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.oi.bean.User; @Controller public class BaseController { @Autowired private SessionFactory sessionFactory; @RequestMapping(value="/") public String getRespnse(Model m) { Session session =sessionFactory.getCurrentSession(); // List<User> usrLst=(List<User>) session.createQuery("from User"); // System.out.println("IN"+usrLst); return "home"; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
Please help. Continue!!!
Solution
@RequestMapping(value="/")
@RequestMapping(value="/") @Transactional public String getRespnse(Model m) { Session session =sessionFactory.getCurrentSession(); // List<User> usrLst=(List<User>) session.createQuery("from User"); // System.out.println("IN"+usrLst); return "home"; }
>Use your controller method to add @ transactional > to delete @ enabletransactionmanagement to the user class
Try it. I think it will be useful to you
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码