如何在JPA 2.1中指定实体映射?

时间:2013-06-14 09:34:39

标签: java jpa orm

JPA 2.0的实体映射文件的正确起始标记是

<entity-mappings version="1.0" xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">

JPA 2.1需要更正哪些内容?

我试过

<entity-mappings version="2.1" xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm">

但这会产生错误:

  

文档中没有引用语法约束(DTD或XML Schema)。

3 个答案:

答案 0 :(得分:7)

根据JPA 2.1规范所说的可能;-)或docs of a JPA 2.1 implementation that tells you

将java.sun.com更改为 xmlns.jcp.org

将orm_1_0更改为 orm_2_1

将版本=“1.0”更改为版本=“2.1”

答案 1 :(得分:6)

根据official documentation 12.3 XML架构部分:

<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
      http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd"
    version="2.1">
      ...
</entity-mappings>

答案 2 :(得分:1)

对于2.1版,以下工作正常:

<?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">