简体   繁体   中英

Map autoIncrement non-primary key in hibernate

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 ie 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.

To solve this problem: "I want it to insert the Subject table with actual autoIncremented TEACHER_ID."

I would suggest keeping a sequence object in the database to manually track the increment.

To generate ID automatically using Hibernate:

1) Create a sequence object in your database developer(SQL developer).

2) Add this code to id variable:

  @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ONE") @SequenceGenerator(name = "ONE", sequenceName = "ONE", allocationSize =1) int id; 

3) 'ONE' is the name of the sequence, I have created you can name it whatever you want and allocation Size is the increment by number.

4) When inserting the values into the database (Teacher ID) send 0 in place of Id and Hibernate will automatically translate the value from the sequence number. Now you can use this number to populate the subject table.

Note: the ID would be returned when inserting a new object in the teacher table. Using that ID you can populate the Subject table. it is sometimes better to do it old way rather than using the already provided annotations by spring.

Hope this saves your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM