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を使用してJPA2.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 に答える