我想发出POST请求以将用户保存到我的Oracle DB中。
我在jpa和hibernate中使用Spring MVC。
这是我的jpaContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.licenta.ascourses" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle8iDialect" />
<entry key="hibernate.hbm2ddl.auto" value="create-drop" />
<entry key="hibernate.format_sql" value="true" />
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:oracle" />
<property name="username" value="user" />
<property name="password" value="pass" />
</bean>
</beans>
这是我的UserRepoImpl类:
package com.licenta.ascourses.repositories.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.licenta.ascourses.model.User;
import com.licenta.ascourses.repositories.UserRepo;
@Repository("userRepository")
public class JpaUserRepo implements UserRepo {
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;
public User getUserById(Long id) {
return em.find (User.class, id);
}
public List<User> getAllUsers() {
return null;
}
public User createUser(User user) {
em.persist(user);
em.flush();
return user;
}
}
这是UserServiceImpl类:
package com.licenta.ascourses.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.licenta.ascourses.model.User;
import com.licenta.ascourses.repositories.UserRepo;
import com.licenta.ascourses.repositories.jpa.JpaUserRepo;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepo userRepo;
public User getUserById(Long id) {
return userRepo.getUserById(id);
}
public List<User> getAllUsers() {
return userRepo.getAllUsers();
}
@Transactional
public User createUser(User user) {
return userRepo.createUser (user);
}
}
这是来自REST控制器的方法:
@Autowired
private UserService userService;
@RequestMapping(method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_UTF8_VALUE)
public User saveUser (@RequestBody User user) {
return userService.createUser(user);
}
我在服务图层上有@Transactional
注释。我尝试了许多在线找到的解决方案,使用存储库层上的@Transactional,或者使用@Scope(proxyMode = ScopedProxyMode.INTERFACES)到存储库或服务类。没有什么对我有用。我是Spring和Hibernate的新手,所以欢迎所有的答案或建议
这是我尝试发帖时遇到的一个例外:
PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [ascoursesServlet] in context with path [/ASCourses] threw exception [Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress] with root cause
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1136)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1297)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:507)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy24.flush(Unknown Source)
at com.licenta.ascourses.repositories.jpa.JpaUserRepo.createUser(JpaUserRepo.java:32)
at com.licenta.ascourses.service.UserServiceImpl.createUser(UserServiceImpl.java:33)
at com.licenta.ascourses.controller.TestController.saveUser(TestController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:507)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:785)
我终于找到了适合我的解决方案:
我已经移动了<代码>
到我的servlet配置。xml文件。
答案如下:
@事务性在SpringWebMVC中不起作用?
在SpringMVCWeb应用程序中,试图调用“persist”方法将实体模型保存到数据库时,我遇到了这个错误。在internet上找不到任何与此特定错误相关的帖子或页面。看起来EntityManagerFactoryBean有点问题,但我对Spring编程相当陌生,所以对我来说,根据web上的各种教程文章,似乎一切都初始化得很好。 调度器servlet。xml 注册控制器。JAVA
问题内容: 当尝试调用“ persist”方法将实体模型保存到Spring MVC Web应用程序中的数据库时,出现此错误。在Internet上找不到与该特定错误相关的任何帖子或页面。似乎似乎EntityManagerFactory bean出了点问题,但是我对Spring编程还是比较陌生,所以对我来说,似乎一切都已初始化好,并且根据Web上的各种教程文章。 dispatcher-servlet.
列出需要保存的数据。保存前必须删除现有数据并保存新数据。如果有任何删除 在这里,每次删除都会添加新事务 在删除时,它会引发异常“由以下原因引起:javax.persistence.TransactionRequiredException:没有具有实际事务的实体管理器可用于当前线程 - 无法可靠地处理'remove'调用” 我可以通过添加一个@ Transactional for public la
尝试使用EntityManager时出错 我使用Spring(不是boot)、jpa和hibernate 有人能帮我吗? 实体课程 Spring配置类 在我得到的地方测试“刀” 使用find complete for error命令行 和我的主班 哦,我的朋友。xml 我在类和方法上使用了@Transactional,结果没有改变 请帮帮我,我已经做了所有的选择。
此错误发生在,当方法运行时。 向添加注释不能解决问题-https://stackoverflow.com/a/32552558/3536552 你知道怎么修吗?
问题:原因异常。 代码: