【问题标题】:persistence.xml file is not in the classpathpersistence.xml 文件不在类路径中
【发布时间】:2018-05-16 13:33:41
【问题描述】:

所以我正在尝试使用 Wildfly、JPA、Hibernate 和 MariaDB 在 Eclipse 中使用 Maven 构建一个管理系统。我在 WildFly 管理控制台中配置了我的数据源,一切都很好。 Succesful connection picture in WildFly 现在我正在尝试使用项目中的 Eclipse 中的 Entity Manager Factory 写入数据库。但我收到了这个错误信息: Error message in Eclipse 在这里你可以看到我的项目树: Project tree

这是我的 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gyurmatag.adminsystem</groupId>
<artifactId>adminsystem</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>

    <servlet.version>3.1.0</servlet.version>
    <jsf.version>2.2.12</jsf.version>
    <primefaces.version>5.3</primefaces.version>

    <version.wildfly>9.0.2.Final</version.wildfly>
    <jboss.home>${project.build.directory}/wildfly-${version.wildfly}
    </jboss.home>
    <server.config>standalone.xml</server.config>

    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-dependency-plugin>2.10</maven-dependency-plugin>
    <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
    <wildfly-maven-plugin.version>1.1.0.Alpha5</wildfly-maven-plugin.version>
</properties>

<dependencies>
    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- JSF -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>${jsf.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>${jsf.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- PrimeFaces -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>${primefaces.version}</version>
    </dependency>       

    <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-
client -->
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-
entitymanager -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>

</dependencies>

<build>
<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <!-- Unpack the distribution -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <executions>
                <execution>
                    <id>unpack-wildfly</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.wildfly</groupId>
                                <artifactId>wildfly-dist</artifactId>
                                <version>${version.wildfly}</version>
                                <type>zip</type>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}
</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Copy server configuration -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-standalone-xml</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>

<outputDirectory>${jboss.home}/standalone/configuration</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Maven WildFly plugin -->
        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>${wildfly-maven-plugin.version}</version>
            <configuration>
                <jbossHome>${jboss.home}</jbossHome>
                <serverConfig>${server.config}</serverConfig>
            </configuration>
        </plugin>
    </plugins>
    </pluginManagement>
</build>

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">

           <persistence-unit name="adminsystem" transaction-type="JTA">
           <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

               <jta-data-source>jboss/MariaDBDS</jta-data-source>


               <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                     <property name="hibernate.hbm2ddl.auto" value="validate" />
                     <property name="hibernate.show_sql" value="true" />
                     <property name="hibernate.format_sql" value="true" />
                     <property name="hibernate.connection.release_mode" value="auto" />
               </properties>
           </persistence-unit>

这是我的 JPAUtility 课程:

package com.gyurmatag.adminsystem.model;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class JPAUtility {
private static final EntityManagerFactory emFactory;
static {
       emFactory = Persistence.createEntityManagerFactory("adminsystem");
}
public static EntityManager getEntityManager(){
    return emFactory.createEntityManager();
}
public static void close(){
    emFactory.close();
}
} 

如果您想更深入地了解我的项目,可以在这里找到它:Bitbucket repo

你能帮我解决这个问题吗?

非常感谢! :)

【问题讨论】:

  • persistence.xml 应该叫什么...
  • 这似乎是问题中的错字......
  • 是的。这是一个错字。感谢您的回答!

标签: hibernate maven jpa jakarta-ee wildfly


【解决方案1】:

我怀疑你有太常见的“太多依赖错误”。

将所有依赖项替换为:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>${primefaces.version}</version>
    </dependency>       
</dependencies>

然后看看你的进展如何......

【讨论】:

  • 我的代码中有错字。无论如何感谢您的回答!
  • 无论如何你都应该修复你的依赖关系
  • 你认为哪个更容易维护?
猜你喜欢
  • 2018-01-17
  • 1970-01-01
  • 2012-08-21
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
相关资源
最近更新 更多