我正在构建一个服务,以json格式将数据插入数据库表。 当我以下列形式发布请求时:
{"orderID":123,"customerId":44444,"totalAmount":1222.0,"status":"RTGS"}
我收到以下错误:
SEVERE:servlet [spring]的Servlet.service()与路径的上下文 [/ oms]引发异常[请求处理失败;嵌套异常是 org.springframework.orm.hibernate4.HibernateSystemException:不能 获取当前线程的事务同步会话;嵌套 异常是org.hibernate.HibernateException:无法获取 具有根本原因的当前线程的事务同步会话 org.hibernate.HibernateException:无法获取事务 - 当前线程的同步会话 org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) 在 org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) 在com.exclusively.oms.dao.OrderDAOIml.addOrders(OrderDAOIml.java:31)
我的OrderDAOiml.java
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.exclusively.oms.entity.Order;
@Repository
public class OrderDAOIml implements OrderDAO{
private static final Logger logger = LoggerFactory.getLogger(OrderDAOIml.class);
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void addOrders(Order o)
{
System.out.println("In Add orders");
Session session = sessionFactory.getCurrentSession();
session.save(o);
logger.info("Orders saved successfully. Orders details = " + o);
}
}
OrderServiceIml.java
package com.exclusively.oms.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.exclusively.oms.dao.OrderDAO;
import com.exclusively.oms.entity.Order;
@Service("orderService")
public class OrderServiceIml implements OrderService{
@Autowired
private OrderDAO orderDAO;
public void setOrdersDAO(OrderDAO ordersDAO) {
this.orderDAO = ordersDAO;
}
@Override
public void addOrders(Order o) {
// TODO Auto-generated method stub
this.orderDAO.addOrders(o);
}
}
弹簧servlet.xml中
<context:component-scan base-package="com.exclusively.oms.controller" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/employee_db" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.exclusively.oms.entity.Order</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataDao" class="com.exclusively.oms.dao.OrderDAOIml"> </bean>
<bean id="dataServices" class="com.exclusively.oms.service.OrderServiceIml"></bean>
</beans>