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.
i want it to insert Subject table with actual autoIncremented TEACHER_ID.
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<String, subject> subjectInfo = new HashMap<>(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<String, Subject> getAdditionalInfo() {
return subjectInfo;
}
public void setAdditionalInfo(Map<String, Subject> subjectInfo) {
this.subjectInfo = subjectInfo;
}
}
Other values are inserting properly except TEACHER_ID.
i tried
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
@Column(name = "TEACHER_ID", unique = true)
private Long teacherId;
and this
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TEACHER_ID", unique = true)
private Long teacherId;
No luck. Can anyone tell me what i am missing here.