|  | <html><head> | 
|  | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|  | <title>2.  Unique Constraints</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter 12.  Mapping Metadata"><link rel="prev" href="jpa_overview_mapping.html" title="Chapter 12.  Mapping Metadata"><link rel="next" href="jpa_overview_mapping_column.html" title="3.  Column"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.  | 
|  | Unique Constraints | 
|  | </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping.html">Prev</a> </td><th width="60%" align="center">Chapter 12.  | 
|  | Mapping Metadata | 
|  | </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_mapping_column.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jpa_overview_mapping_unq"></a>2.  | 
|  | Unique Constraints | 
|  | </h2></div></div></div><a class="indexterm" name="d0e9518"></a><a class="indexterm" name="d0e9525"></a><p> | 
|  | Unique constraints ensure that the data in a column or combination of columns is | 
|  | unique for each row. A table's primary key, for example, functions as an | 
|  | implicit unique constraint. In JPA, you represent other unique | 
|  | constraints with an array of <code class="classname"> UniqueConstraint</code> | 
|  | annotations within the table annotation. The unique constraints you define are | 
|  | used during table creation to generate the proper database constraints, and may | 
|  | also be used at runtime to order <code class="literal">INSERT</code>, <code class="literal">UPDATE | 
|  | </code>, and <code class="literal">DELETE</code> statements. For example, suppose there | 
|  | is a unique constraint on the columns of field <code class="literal">F</code>. In the | 
|  | same transaction, you remove an object <code class="literal">A</code> and persist a new | 
|  | object <code class="literal">B</code>, both with the same <code class="literal">F</code> value. The | 
|  | JPA runtime must ensure that the SQL deleting <code class="literal">A</code> | 
|  | is sent to the database before the SQL inserting <code class="literal">B</code> to avoid a | 
|  | unique constraint violation. | 
|  | </p><p> | 
|  | <code class="classname">UniqueConstraint</code> has a single property: | 
|  | </p><div class="itemizedlist"><ul type="disc"><li><p> | 
|  | <code class="literal">String[] columnNames</code>: The names of the columns the | 
|  | constraint spans. | 
|  | </p></li></ul></div><p> | 
|  | In XML, unique constraints are represented by nesting <code class="literal"> | 
|  | unique-constraint</code> elements within the <code class="literal"> table</code> | 
|  | element. Each <code class="literal">unique-constraint</code> element in turn nests | 
|  | <code class="literal">column-name</code> text elements to enumerate the contraint's | 
|  | columns. | 
|  | </p><div class="example"><a name="jpa_overview_mapping_unq_attrex"></a><p class="title"><b>Example 12.2.  | 
|  | Defining a Unique Constraint | 
|  | </b></p><div class="example-contents"><p> | 
|  | The following defines a unique constraint on the <code class="literal"> TITLE</code> | 
|  | column of the <code class="literal">ART</code> table: | 
|  | </p><pre class="programlisting"> | 
|  | @Entity | 
|  | @Table(name="ART", uniqueConstraints=@Unique(columnNames="TITLE")) | 
|  | public class Article { | 
|  | ... | 
|  | } | 
|  | </pre><p> | 
|  | The same metadata expressed in XML form: | 
|  | </p><pre class="programlisting"> | 
|  | <entity class="org.mag.Article"> | 
|  | <table name="ART"> | 
|  | <unique-constraint> | 
|  | <column-name>TITLE</column-name> | 
|  | </unique-constraint> | 
|  | </table> | 
|  | ... | 
|  | </entity> | 
|  | </pre></div></div><br class="example-break"></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_mapping.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_mapping.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_mapping_column.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 12.  | 
|  | Mapping Metadata | 
|  |  </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 3.  | 
|  | Column | 
|  | </td></tr></table></div></body></html> |