我的程序运行得非常好,直到我开始使用持久性和EntityManager
@Inject
。
抛出了很多错误,但我认为这个错误最多。
它讲述了一个意想不到的元素,但我在网上找不到任何有用的东西。
日志:
Connected to server
[2017-05-16 09:13:17,808] Artifact web-example:war exploded: Artifact is being deployed, please wait...
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomcatWebAppBuilder deployWebApps
INFO: using default host: localhost
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomcatWebAppBuilder init
INFO: ------------------------- localhost -> /
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomEEClassLoaderEnricher validateJarFile
WARNING: jar '/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar' contains offending class: javax.persistence.Entity. It will be ignored.
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomEEClassLoaderEnricher validateJarFile
WARNING: jar '/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/lib/jboss-transaction-api_1.1_spec-1.0.1.Final.jar' contains offending class: javax.transaction.Transaction. It will be ignored.
mei 16, 2017 9:13:18 PM org.apache.openejb.config.ConfigurationFactory configureApplication
INFO: Configuring enterprise application: /Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT
mei 16, 2017 9:13:18 PM org.apache.openejb.config.ReadDescriptors deploy
SEVERE: Unable to load Persistence Unit from EAR: /Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT, module: file:/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/classes/. Exception: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are <{http://java.sun.com/xml/ns/persistence}persistence>
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are <{http://java.sun.com/xml/ns/persistence}persistence>
/resources/META-INF/persistence.xml
<persistence 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
http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit
name="mysql"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>
nl.han.dea.domain.User
</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/Spotitube?autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
<property name="hibernate.connection.username" value="spotitube"/>
<property name="hibernate.connection.password" value="java"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
的pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>nl.han.dea</groupId>
<artifactId>web-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>web-example</name>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.6.Final</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>
答案 0 :(得分:2)
问题在日志输出的这一行显而易见:
异常:意外元素(uri:“http://xmlns.jcp.org/xml/ns/persistence”,local:“persistence”)。
预期元素是&lt; {{{}}}持久性
这告诉我们,它们与JPA版本2.0和2.1不匹配。这种冲突的原因在于您在pom.xml
中提到的相当旧的Hibernate版本。
Hibernate 4.2.x仅支持JPA 2.0,而TomEE 7.0.x需要一个JPA 2.1提供程序,因为你在问题中提到了persistence.xml
,所以
持久性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://java.sun.com/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd http://xmlns.jcp.org/xml/ns/persistence“ 版本= “2.1” &GT;
有关JPA支持Hibernate的更多详细信息,请参阅列出这些信息的http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd下载页面。
<强>解决方案强>
最有可能的是,将Hibernate的版本更改为4.3.x可以解决您遇到的一些问题。以下是相关依赖声明的外观:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
如果您想进一步实现应用程序的现代化,请尝试使用Hibernate 5.2.x.对于当前版本,依赖性声明如下所示:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency
希望它有所帮助。
添加 - 亮强>
如果您想在TomEE&gt; = 7.0.3设置中使用Hibernate 5.2.x,建议您添加
<property name="tomee.jpa.cdi" value="false" />
到你的persistence.xml
,否则容器会在启动时抱怨注射问题。有关详情,请参阅Hibernate ORM以及this post。
此外,将<provider>
条目更改为新的/正确的包,如下所示:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>