Caused by: 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>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:740)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
我正在尝试构建一个简单的osgi jpa 2.1 模块,该模块将在Glassfish 4.1上运行并使用Postgres 9.4。 GF上的JDBC资源已经创建并且工作正常(我的意思是“ping”按钮),以及在META-INF目录上写一个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="something" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/myjdbc</jta-data-source>
<properties>
<property name="javax.persistence.schema-genaration" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
最有趣的是 - 如果我将jpa 2.1模式替换为jpa 2.0,它将正常工作,但只能无法从实体类创建表。
那么,如何解决这个问题并使用JPA 2.1 xml架构。
Add.information:
新闻实体在这里:
@Entity
public class News {
@Id
@GeneratedValue
private Long ID;
private String title;
private String content;
private Long views;
// getters and setters.... default non-arg constructor was omitted
我的pom.xml文件:
<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">
<parent>
<artifactId>myTestProject</artifactId>
<groupId>com.myTestProject</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myTestProject.module.domain</groupId>
<artifactId>module.domain</artifactId>
<packaging>bundle</packaging>
<name>module.domain</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>com.myTestProject.module.domain.*</Export-Package>
<Import-Package>javax.persistence.*, org.osgi.framework</Import-Package>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
<Bundle-Activator>com.myTestProject.module.domain.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>