blob: 76522bc549a746ca9a0795b8e69573d876272397 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.&nbsp; Unique Constraints</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.3 User's Guide"><link rel="up" href="jpa_overview_mapping.html" title="Chapter&nbsp;13.&nbsp; Mapping Metadata"><link rel="prev" href="jpa_overview_mapping.html" title="Chapter&nbsp;13.&nbsp; Mapping Metadata"><link rel="next" href="jpa_overview_mapping_column.html" title="3.&nbsp; 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.&nbsp;
Unique Constraints
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_mapping.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;13.&nbsp;
Mapping Metadata
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_mapping_column.html">Next</a></td></tr></table><hr></div><div class="section" title="2.&nbsp; Unique Constraints"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="jpa_overview_mapping_unq">2.&nbsp;
Unique Constraints
</h2></div></div></div>
<a class="indexterm" name="d5e5471"></a>
<a class="indexterm" name="d5e5475"></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 these properties:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
<p>
<code class="literal">String name</code>: The name of the constraint. OpenJPA will choose
a name if you do not provide one, or will create an anonymous constraint.
</p>
</li><li class="listitem">
<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 constraint's
columns.
</p>
<div class="example"><a name="jpa_overview_mapping_unq_attrex"></a><p class="title"><b>Example&nbsp;13.2.&nbsp;
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=@UniqueConstraint(name="TITLE_CNSTR", columnNames="TITLE"))
public class Article {
...
}
</pre>
<p>
The same metadata expressed in XML form:
</p>
<pre class="programlisting">
&lt;entity class="org.mag.Article"&gt;
&lt;table name="ART"&gt;
&lt;unique-constraint&gt;
&lt;name&gt;TITLE_CNSTR&lt;/name&gt;
&lt;column-name&gt;TITLE&lt;/column-name&gt;
&lt;/unique-constraint&gt;
&lt;/table&gt;
...
&lt;/entity&gt;
</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>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_mapping.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_mapping_column.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;13.&nbsp;
Mapping Metadata
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;3.&nbsp;
Column
</td></tr></table></div></body></html>