title: Map autoIncrement non-primary key in hibernate
body: <p>I have a teacher table, where it contains one primary key TEACHER_UNIQUE_ID,and other one autoIncrement key with Index TEACHER_ID.
  Now i have to map autoIncrement key to other table i.e SUBJECT . I have used below code, but this always sets TEACHER_ID as null in subject class. </p>
  
  <p>i want it to insert Subject table with actual autoIncremented TEACHER_ID.</p>
  
  <pre><code>public class Teacher {
  
  @Id  
  @Column(name = "TEACHER_UNIQUE_ID")  
  private String teacherUniqueId;
  
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "TEACHER_ID", unique = true)
  private Long teacherId;
  -----
  -----
  @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="teacher" ,orphanRemoval = true)
  @Fetch(FetchMode.SELECT)
  @MapKey(name = "fieldName")
  private Map&lt;String, subject&gt; subjectInfo  = new HashMap&lt;&gt;(0); 
  
  public Long getTeacherUniqueId() {
      return teacherUniqueId;
  }
  public void setTeacherUniqueId(Long teacherUniqueId) {
      this.teacherUniqueId = teacherUniqueId;
  }
  
  public Long getTeacherId() {
      return teacherId;
  }
  public void setTeacherId(Long teacherId) {
      this.teacherId = teacherId;
  }
  
  private void setField(String key, String value) {
      subject subjectInfoData = subjectInfo.get(key);
      if(subjectInfoData == null){
          subjectInfoData = new subject();
          subjectInfoData.setFieldName(key);                  
          subjectInfo.put(key, subjectInfoData);
      }
      subjectInfoData.setTeacher(this);
      **subjectInfoData.setId(this.getTeacherId());**  -- its inserting null to id in SUBJECT table. i want it to insert actual TEACHER_ID got from TEACHER Table.
      subjectInfoData.setFieldValue(value);
      setAdditionalInfo(subjectInfo);
  }
  
  public String getCustomFieldValue(String fieldName) {
      return subjectInfo.get(fieldName)!=null?subjectInfo.get(fieldName).getFieldValue():null;
  }
  public void setCustomFieldValue(String fieldName, String fieldValue) {
      setField(fieldName, fieldValue);
  }
  
  public Map&lt;String, Subject&gt; getAdditionalInfo() {
      return subjectInfo;
  }
  
  public void setAdditionalInfo(Map&lt;String, Subject&gt; subjectInfo) {
      this.subjectInfo = subjectInfo;
  }
  
  }
  </code></pre>
  
  <p>Other values are inserting properly except TEACHER_ID. </p>
  
  <p>i tried </p>
  
  <pre><code>@GenericGenerator(name="generator", strategy="increment")
  @GeneratedValue(generator="generator")
  @Column(name = "TEACHER_ID", unique = true)
  private Long teacherId;
  </code></pre>
  
  <p>and this</p>
  
  <pre><code>@GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "TEACHER_ID", unique = true)
  private Long teacherId;
  </code></pre>
  
  <p>No luck. Can anyone tell me what i am missing here.</p>