使用Spring,JPA,Restful Web Service和JSon。错误就在这里。
Exception Description: No transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:87) [eclipselink-2.5.1.jar:2.5.1.v20130918-f2b9fc5]
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50) [eclipselink-2.5.1.jar:2.5.1.v20130918-f2b9fc5]
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:2041) [eclipselink-2.5.1.jar:2.5.1.v20130918-f2b9fc5]
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:863) [eclipselink-2.5.1.jar:2.5.1.v20130918-f2b9fc5]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_25]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) [spring-orm.jar:3.1.1.RELEASE]
at $Proxy24.flush(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_25]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) [spring-orm.jar:3.1.1.RELEASE]
at $Proxy24.flush(Unknown Source) at com.hs.rest.springController.user.persistance.UserDAO.insertUser(UserDAO.java:62) [classes:]
at com.hs.rest.springController.user.service.UserService.addNewUser(UserService.java:65) [classes:]
at com.hs.rest.springController.StateController.getDefaultMovie(StateController.java:28) [classes:]
我的Restservice Controller类
@Controller
public class StateController {
private static final Logger logger = Logger.getLogger(StateController.class);
@Resource(name = "UserService")
private IUserService userService;
@RequestMapping(value = "/movie", method = RequestMethod.GET)
public @ResponseBody
User getDefaultMovie() {
logger.debug("getDefaultMovie() Method is started !");
System.out.println("getDefaultMovie() Method is started !");
User user = new User();
user.setName("TEST");
userService.addNewUser(user);
logger.debug("getDefaultMovie() Method is finished ! " + user.getName());
System.out.println("getDefaultMovie() Method is finished ! " + user.getName());
return user;
}
}
服务类
@Service("UserService")
public class UserService implements IUserService {
private Logger logger = Logger.getLogger(this.getClass());
@Resource(name = "UserDAO")
private IUserDAO userDAO;
@Transactional(propagation = Propagation.REQUIRED)
public void addNewUser(User user) {
try {
logger.debug("insertUser() method has been started.");
user = userDAO.insertUser(user);
logger.debug("insertUser() method has been finished.");
} catch (DAOException e) {
logger.error("insertUser() method has been failed.");
throw new SystemException(e.getErrorCode(), "Faield to add new User.", e);
}
}
}
配置设置在这里。 的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns"
id="WebApp_ID" version="3.0">
<display-name>restful</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.hs.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-beans.xml</param-value>
</context-param>
我的问题是如何让交易活跃起来。 请帮助我。抱歉我的英语和问题经验。 :)
答案 0 :(得分:0)
确保您的服务上有@Transactional。