title: How to put unique constraint on a foreign key in JPA
body: <p>Using JPA (hibernate, spring tool suite) I'm trying to make a uniqueness constraint on two table columns, one of which is a foreign key.  I can't figure out the syntax.</p>
  
  <p>I tried.</p>
  
  <pre><code>@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"company.companyId" , "state"})})
  @Entity
  public class Branch {
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private int branchId;
      @ManyToOne(optional = false)
      private Company company;
      @Column
      int state;
  }
  </code></pre>
  
  <p>but I get error:</p>
  
  <pre><code>database column 'company.companyId' 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)
  </code></pre>
  
  <p>so I tried several other ways, such as "company_id" and "company_company_id" and "company_companyId".  None of them work.</p>