I found the solution.
To unique constraint a column I needed to use the @Column annotation's
attributes:
@Entity
public class Account {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer accountId = null;
@Basic @Column(unique=true,nullable=false)
private String accountName = null;
@Basic
private Date creationDate = null;
@ManyToMany(cascade=CascadeType.ALL)
private List<Host> hostList = new ArrayList<Host>();
Thanks for the Help Ravi!
Thomas
On Jan 19, 2010, at 10:13 PM, Thomas Polliard wrote:
> @Entity
> @Table(name="ACCOUNT",uniqueconstrain...@unique(columnNames="ACCOUNTNAME"))
> public class Account {
> @Id
> @GeneratedValue(strategy=GenerationType.AUTO)
> private Integer accountId = null;
> @Basic
> private String accountName = null;
> @Basic
> private Date creationDate = null;
> @ManyToMany(cascade=CascadeType.ALL)
> private List<Host> hostList = new ArrayList<Host>();
>
> ....
>
>
> Results in
> compile:
> [javac] Compiling 4 source files to /Users/polliard/Workspace/Example
> JPA/build
> [javac] /Users/polliard/Workspace/Example
> JPA/src/com/trukoda/examples/jpa/Account.java:19: incompatible types
> [javac] found : org.apache.openjpa.persistence.jdbc.Unique
> [javac] required: javax.persistence.UniqueConstraint
> [javac]
> @Table(name="ACCOUNT",uniqueconstrain...@unique(columnNames="ACCOUNTNAME"))
> [javac] ^
> [javac] Note: /Users/polliard/Workspace/Example
> JPA/src/com/trukoda/examples/jpa/MainQuery.java uses unchecked or unsafe
> operations.
> [javac] Note: Recompile with -Xlint:unchecked for details.
> [javac] 1 error
>
>
> So if that example does exist it must not compile correctly.
>
> Thanks for taking a look though.
>
> Thomas
> On Jan 19, 2010, at 10:01 PM, Ravi Palacherla wrote:
>
>> Hi,
>>
>> http://java.sun.com/javaee/5/docs/api/index.html?javax/persistence/Table.html
>>
>> As per the above link the one mentioned in openJPA document is supposed to
>> work.
>> (http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_unique)
>>
>> I also saw the following in openJPA example:
>>
>> @Entity
>> @Table(name="ART", uniqueconstrain...@unique(columnNames="TITLE"))
>> public class Article {
>> ...
>> }
>>
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_mapping_identityex
>>
>> Please try if the above works.
>>
>> Regards,
>> Ravi.
>>
>> -----Original Message-----
>> From: Thomas Polliard [mailto:[email protected]]
>> Sent: Tuesday, January 19, 2010 7:50 PM
>> To: [email protected]
>> Subject: Question about Unique Contraint
>>
>> public class Account {
>> @Id
>> @GeneratedValue(strategy=GenerationType.AUTO)
>> private Integer Id = null;
>> @Unique
>> private String accountName = null;
>> @Basic
>> private Date creationDate = null;
>> @ManyToMany(cascade=CascadeType.ALL)
>> private List<Host> hostList = new ArrayList<Host>();
>>
>> However, in the database it actually doesn't restrict the accountName but
>> rather forces a new ID to be generated with the same accountName. What page
>> can I find out about creating a Unique constraint that mimics this in pseudo
>> database ddl
>>
>> create table Account (
>> Id int4 PRIMARY KEY,
>> accountname varchar(255) NOT NULL UNIQUE,
>> .....
>>
>>
>> Thanks for the assistance.
>>
>> Thomas
>>
>>
>> PS:
>>
>> http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_unique
>>
>> Doesn't appear to be the proper Annotation.
>>
>