我使用的是Spring 4.3.8和Hibernate 5.2.10,我尝试在数据库中创建表并插入记录但我收到错误
Dao Class
@Transactional
public void saveDayBookData(DayBookData dayBook){
hibernateTemplate.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
hibernateTemplate.saveOrUpdate(dayBook);
}
适用于所有spring和hibernate配置
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db1?autoReconnect=true&useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="packagesToScan" value="entityClasses"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<bean id="vendor" class="vendors.VendorDao">
<property name="hibernateTemplate" ref="template"></property>
</bean>
<bean id="dayBook" class="accountBooks.DayBookDao">
<property name="hibernateTemplate" ref="template"></property>
</bean>
</beans>
主要课程
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r = new ClassPathResource("applicationContext.xml");
BeanFactory bean = new XmlBeanFactory(r);
DayBookDao dayBookDao = (DayBookDao) bean.getBean("dayBook");
DayBookData dayBook = new DayBookData();
dayBook.setAccountType("Bank Account");
dayBook.setTransType("Receipt");
dayBook.setOppAccount("Profit-Loss");
dayBook.setAmount(15000);
dayBook.setTransDate(new Date());
dayBookDao.saveDayBookData(dayBook);
}
我收到此错误
Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:133)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:456)
at accountBooks.DayBookDao.saveDayBookData(DayBookDao.java:22)
at accountBooks.TestDayBook.main(TestDayBook.java:28)
答案 0 :(得分:0)
看起来问题就是你创建DAO的方式。
而是将扫描添加到applicationContext.xml
<context:component-scan base-package="com.your.package.here" />
使用@Repository
注释DAO类,让Spring为它们创建代理。
删除从xml声明的DAO bean。
答案 1 :(得分:0)
我的类似问题已通过以下两种方法得到解决。
通过手动处理交易。
Session session = sessionFactory.getCurrentSession(); 事务tx = session.beginTransaction(); UserInfo user =(UserInfo)session.get(UserInfo.class,1); tx.commit();
告诉Spring在您的web.xml过滤器中打开并管理事务并确保使用@Repository @Transactional
&LT;过滤器&gt; &LT; filter-name&gt; hibernateFilter&lt; / filter-name&gt; &LT; filter-class&gt; org.springframework.orm.hibernate5.support.OpenSessionInViewFilter&lt; / filter-class&gt; &LT; init-param&gt; &LT; PARAM-名称&gt;的sessionFactory &LT; PARAM值&GT; session.factory &LT; / init-param&gt; &LT; / filter&gt; &LT; filter-mapping&gt; &LT;过滤器 - 名称&gt;&hibernateFilter LT; / filter-name&gt; &LT; url-pattern的&GT; / *&LT; / url-pattern&gt; &LT; / filter-mapping&gt;
抱歉格式不正确。