如何解决关于 persistence.xml 的错误。

huangapple 未分类评论52阅读模式
英文:

How to run fix error about persistence.xml

问题

以下是翻译好的内容:

hi all,我在运行使用Hibernate的Java应用程序时遇到了两个不同的错误。

  1. 在运行时出现异常:“Exception in thread 'main' javax.persistence.PersistenceException: 没有名为 FastMatch 的 EntityManager 的持久化提供程序。”
  2. 造成的错误:java.lang.ClassNotFoundException: 无法加载请求的类:com.mysql.jdbc.Driver

对于第一个错误,我已经像下面这样将我的 persistence.xml 添加进去了:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="FastMatch" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.dxtr.hibernate.newOrderSingleEntity</class>
        <properties>
            <property name="eclipselink.logging.level" value="INFO"/>
            <property name="eclipselink.logging.level.sql" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://url/tables"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="eclipselink.logging.level.connection" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

对于第二个错误,我已经在我的依赖项中添加了:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
</dependency>

然而,我仍然遇到了类似以下错误:

Exception in thread "main" javax.persistence.PersistenceException: 没有名为 FastMatch 的 EntityManager 的持久化提供程序。
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
            at com.dxtr.Transaction.FastMatchTransaction.FastmatchTransaction.main(FastmatchTransaction.java:12)

此外,这是我的 pom.xml 文件内容:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.2.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.4.2.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>

我尝试了在其他链接中找到的解决方案,甚至添加了 javax.persistence.2.2.jar,但问题仍然无法解决。更多信息,我正在使用MySQL 5.7 和 Eclipse 作为IDE。在AWS上运行此代码,但是无法解决问题。请问如何解决这个问题?

fuad

英文:

hi all i got two different error when i run my java apps using hibernate persistence.xml

  1. Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
    2.Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver

for the first error i have add my persistence.xml like this one :
here my persistence.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;persistence version=&quot;2.1&quot;
    xmlns=&quot;http://xmlns.jcp.org/xml/ns/persistence&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd&quot;&gt;
    &lt;persistence-unit name=&quot;FastMatch&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
        &lt;class&gt;com.dxtr.hibernate.newOrderSingleEntity&lt;/class&gt;
        &lt;properties&gt;
            &lt;property name=&quot;eclipselink.logging.level&quot; value=&quot;INFO&quot;/&gt;
             &lt;property name=&quot;eclipselink.logging.level.sql&quot; value=&quot;FINE&quot;/&gt;
            &lt;property name=&quot;eclipselink.logging.parameters&quot; value=&quot;true&quot;/&gt; 
            &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.url&quot; value=&quot;jdbc:mysql://url/tables&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;user&quot; /&gt;
            &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;password&quot; /&gt;
            &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;false&quot; /&gt;
            &lt;property name=&quot;hibernate.format_sql&quot; value=&quot;true&quot; /&gt;
            &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQL5Dialect&quot; /&gt;
            &lt;property name=&quot;eclipselink.logging.level.connection&quot; value=&quot;FINEST&quot;/&gt;
        &lt;/properties&gt;
    &lt;/persistence-unit&gt;
&lt;/persistence&gt;

for the second i have add in my depedency

&lt;dependency&gt;
    &lt;groupId&gt;mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;version&gt;5.1.48&lt;/version&gt;
&lt;/dependency&gt;

and
still got error like this one

Exception in thread &quot;main&quot; javax.persistence.PersistenceException: No Persistence provider for EntityManager named FastMatch
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at com.dxtr.Transaction.FastMatchTransaction.FastmatchTransaction.main(FastmatchTransaction.java:12)

and here my pom.xml

 &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;version&gt;5.4.2.Final&lt;/version&gt;
    &lt;/dependency&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;5.4.2.Final&lt;/version&gt;
    &lt;/dependency&gt;
     &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
        &lt;version&gt;1.0.0.Final&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
  &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
  &lt;version&gt;2.5.0&lt;/version&gt;
&lt;/dependency&gt;

i have try add javax.persistence.2.2.jar and other solution from other link here but this problem cannot run well

for more information i use mysql5.7 and eclipse for IDE. and for running this code i use aws. i have try all of the code in stackoverflow but still got the problem so how to fix it ?

fuad

huangapple
  • 本文由 发表于 2020年4月8日 16:38:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/61096605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定