3

我正在使用 JPA 2.0 并希望使用 XML 而不是注释来创建唯一约束。

带注释的类如下所示:

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    @Column(unique=true)
    private String name;

    // ..
}

像这样的orm.xml文件 - 虽然它缺少唯一约束:

<entity-mappings 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 orm_2_0.xsd"
    version="2.0">
    <entity class="kiosk.model.Person">
        <attributes>
            <id name="id">
                <generated-value strategy="AUTO" />
            </id>
            <basic name="name" />

            <!-- .. -->
        </attributes>
    </entity>
</entity-mappings>

如何使用 XML 向 JPA 2.0 类添加唯一约束?

4

1 回答 1

10
<basic name="name">
    <column unique="true"/>
</basic>

见,http ://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes

于 2012-04-11T13:32:09.733 回答