title: JPA: How to express a unique constraint including an @ElementCollection column?
body: <p>This is my JPA entity class:</p>
  
  <pre><code>@Cacheable
  @Entity
  public class JpaItemSearchRequest implements ItemSearchRequest {
  
      @Id
      @GeneratedValue
      private long id;
  
      @Enumerated(EnumType.STRING)
      private SearchIndex searchIndex;
  
      private String browseNode;
      private String keywords;
  
      @Enumerated(EnumType.STRING)
      @ElementCollection
      private Set&lt;ResponseGroup&gt; responseGroups = new HashSet&lt;ResponseGroup&gt;();
  
      private int itemPage;
  
      @Enumerated(EnumType.STRING)
      private Locale locale;
  
      ... end of fields ...
  }
  </code></pre>
  
  <p><code>SearchIndex</code>, <code>ResponseGroup</code>, and <code>Locale</code> are enums.</p>
  
  <p>This class has a rather complex unique constraint: a combination of
  <code>(searchIndex, browseNode, keywords, responseGroups, itemPage, locale)</code> is considered unique.</p>
  
  <p>So I added the following constraint annotation to the class:</p>
  
  <pre><code>@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "searchIndex", "browseNode", "keywords", "responseGroups", "itemPage", "locale" }) })
  </code></pre>
  
  <p>When I try to exercise my class, I get the following exception:</p>
  
  <pre><code> org.hibernate.AnnotationException: Unable to create unique key constraint (searchIndex, browseNode, keywords, responseGroups, itemPage, locale) on table JpaItemSearchRequest: database column responseGroups not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
      at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1584) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1386) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final]
      at org.hibernate.ejb.EntityManagerFactoryImpl.&lt;init&gt;(EntityManagerFactoryImpl.java:94) ~[hibernate-entitymanager-4.1.7.Final.jar:4.1.7.Final]
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ~[hibernate-entitymanager-4.1.7.Final.jar:4.1.7.Final]
      ... 74 common frames omitted
  </code></pre>
  
  <p>(I use <em>Hibernate 4.1.7.Final</em> as the JPA implementation/service provider)  </p>
  
  <p>The exception gist is: <code>database column responseGroups not found.</code></p>
  
  <p>If I don't specify that constraint, behind the scenes, for my single entity class, Hibernate generates two tables: One for all the "simple" entity data, and one additional table for the <code>Set&lt;ResponseGroup&gt; responseGroups</code> entity data:</p>
  
  <pre><code>CREATE TABLE `jpaitemsearchrequest` (
      `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
      `browseNode` VARCHAR(255) NULL DEFAULT NULL,
      `itemPage` INT(11) NOT NULL,
      `keywords` VARCHAR(255) NULL DEFAULT NULL,
      `locale` VARCHAR(255) NULL DEFAULT NULL,
      `searchIndex` VARCHAR(255) NULL DEFAULT NULL,
      PRIMARY KEY (`id`)
  )
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB
  AUTO_INCREMENT=3;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>CREATE TABLE `jpaitemsearchrequest_responsegroups` (
      `JpaItemSearchRequest_id` BIGINT(20) NOT NULL,
      `responseGroups` VARCHAR(255) NULL DEFAULT NULL,
      INDEX `FKC217757B99AE0422` (`JpaItemSearchRequest_id`),
      CONSTRAINT `FKC217757B99AE0422` FOREIGN KEY (`JpaItemSearchRequest_id`) REFERENCES `jpaitemsearchrequest` (`id`)
  )
  COLLATE='utf8_general_ci'
  ENGINE=InnoDB;
  </code></pre>
  
  <p>How can I correctly express my constraint?</p>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>This question used to read:<br>
  <strong>JPA: Unique constraint across a Set (...unique constraint on another table)</strong>  </p>
  
  <p>Further thinking about my problem, I'm convinced it can be boiled down to the following question:<br>
  <strong>JPA: How to express a unique constraint including an @ElementCollection column?</strong> </p>