title: Entity mapping using a non primary key column
body: <p>I am using hibernate and Mysql in the java project for persistence.
  I have two entities <strong>Transaction</strong> and <strong>Service</strong>. Transaction is having many to one relation to service. 
  I wanted to use a non primary column(SERVICE_CODE) of type VARCHAR from Service table as a foreign key in the Transaction table. But when I do so I get the following exception.</p>
  
  <pre><code>SQL Error: 1452, SQLState: 23000
  Cannot add or update a child row: a foreign key constraint fails.
  </code></pre>
  
  <p>SERVICE_CODE is defined as non null and unique in database.
  Following example works fine if I use primary key from Service table for mapping.</p>
  
  <pre><code>@Entity
  @Table(name="Transaction")
  public class Transaction {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="TRANSACTION_ID")
      long transactionId;
  
  
      @ManyToOne
      @JoinColumn(name="SERVICE_CODE")
      Service service;
  }
  
  
  @Entity
  @Table(name="SERVICE")
  public class Service {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="SERVICE_ID")
      Long serviceId;
  
      @Column(name="SERVICE_CODE")
      String serviceCode; 
  }
  </code></pre>