[英]Problem with persistence provider in a jpa and struts project
I'm developing a java ee aplication using jpa to control a mysql db and struts. 我正在开发使用jpa来控制mysql db和struts的java ee应用程序。 Unfortunately when I run the project on a tomcat server I get this error.
不幸的是,当我在tomcat服务器上运行项目时,出现此错误。
javax.persistence.PersistenceException: No Persistence provider for EntityManager named prova javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56) javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34) test.testing.initEntityManager(testing.java:37) test.testing.add(testing.java:16) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242) com.googlecode.s2hibernate.struts2.plugin.s2hibernatevalidator.interceptor.HibernateValidatorInterceptor.intercept(HibernateValidatorInterceptor.java:38) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236) org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468) org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
I've already searched online for a solution, but nothing fixed my problem. 我已经在网上搜索了解决方案,但是没有什么可以解决我的问题。 Here are the files:
这些是文件:
src/test/Libro.java 的src /测试/ Libro.java
src/test/testing.java 的src /测试/ testing.java
src/META-INF/persistence.xml SRC / META-INF / persistence.xml中
src/struts.xml SRC / struts.xml中
WebContent/WEB-INF/web.xml 的WebContent / WEB-INF / web.xml文件
WebContent/index.xml 的WebContent / INDEX.XML
Project 项目
View: 视图:
Lib: 库:
Thanks for helping out :) 感谢您的帮助:)
From the screenshot Lib we can see that you have a very "mixed" combination of libraries. 从屏幕快照 Lib中,我们可以看到您有一个非常“混合”的库组合。 Here lies the problem!
问题就在这里! You can not mix different API levels of the JPA.
您不能混合使用JPA的不同API级别。 You mixed the very old/early release version 1.x with the most recent variant of JPA 2.2 .
您将非常旧/较早的发行版1.x与JPA 2.2的最新变体混合在一起。
You put both persistence-api-1.0.2.jar
and javax.persistence-api-2.2.jar
into the lib directory within the WEB-INF
of the project. 您将
persistence-api-1.0.2.jar
和javax.persistence-api-2.2.jar
放入了项目的WEB-INF
中的lib目录中。 This causes conflicts at runtime. 这会在运行时导致冲突。
Therefore: 因此:
persistence-api-1.0.2.jar
from the folder and check if that solves the observed exception. persistence-api-1.0.2.jar
并检查是否解决了观察到的异常。 If other exceptions occur, there are more inconsistencies at runtime with your setup. You mis-use the concept of transaction handling in the class testing
within the method createPlaces()
. 您在方法
createPlaces()
中的类testing
误用了事务处理的概念。 Your code is: 您的代码是:
em.persist();
em.getTransaction().begin();
em.getTransaction().commit();
which puts the persist()
call outside of the controlled Transaction you open after that line. 这会将
persist()
调用置于您在该行之后打开的受控事务之外。 Instead of persisting as above, you should better reformulate these lines as below. 与其像上面那样坚持下去,不如按如下方式重新构造这些行。 The snippet moves the
persist
operation within the boundaries of a transaction: 该代码段将
persist
操作移动到事务的边界内:
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
// Only within a tx to prevent inconsistent states in the DB if sth fails here!
em.persist();
tx.commit();
} catch(RuntimeException) {
if(if(tx != null && tx.isActive()) {
tx.rollback();
}
}
For further details check another answer on transaction handling I once wrote. 有关更多详细信息,请查看我曾经写过的有关事务处理的另一个答案 。 It refers to JPA 2.2 and gives you further depth on this topic.
它引用了JPA 2.2,并为您提供了关于此主题的更多深度。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.