JPA 2.0 unique constraint as XML

java, jpa, jpa-2.0

Solution

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

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

Problem

I'm using JPA 2.0 and want to create a unique constraint using XML, not annotations. The annotated class looks like this: ``` @Entity public class Person { @Id @GeneratedValue private Long id; @Column(unique=true) private String name; // .. } ``` And the `orm.xml` file like that - it's missing the unique constraint though: ``` <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> ``` How do I add a unique constraint to a JPA 2.0 class using XML?

Original source