简体   繁体   English

JPA和Struts项目中的持久性提供程序存在问题

[英]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 :) 感谢您的帮助:)

Main issue 主要问题

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.jarjavax.persistence-api-2.2.jar放入了项目的WEB-INF中的lib目录中。 This causes conflicts at runtime. 这会在运行时导致冲突。

Therefore: 因此:

  • Remove the outdated 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. 如果发生其他异常,则在运行时与您的设置存在更多不一致之处。

Other comments & changes 其他评论和更改

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM