在休眠中映射自动增量非主键 [英] Map autoIncrement non-primary key in hibernate

查看:22
本文介绍了在休眠中映射自动增量非主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个教师表,其中包含一个主键 TEACHER_UNIQUE_ID,以及另一个索引为 TEACHER_ID 的自动增量键.现在我必须将 autoIncrement 键映射到其他表,即 SUBJECT .我使用了下面的代码,但这总是在主题类中将 TEACHER_ID 设置为 null.

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.

我希望它插入带有实际 autoIncremented TEACHER_ID 的主题表.

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;
}

}

除 TEACHER_ID 外,其他值都正确插入.

Other values are inserting properly except TEACHER_ID.

我试过了

@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
@Column(name = "TEACHER_ID", unique = true)
private Long teacherId;

还有这个

@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TEACHER_ID", unique = true)
private Long teacherId;

没有运气.谁能告诉我我在这里缺少什么.

No luck. Can anyone tell me what i am missing here.

推荐答案

解决这个问题:我希望它插入带有实际 autoIncremented TEACHER_ID 的主题表."

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.

使用 Hibernate 自动生成 ID:

To generate ID automatically using Hibernate:

1) 在您的数据库开发人员(SQL 开发人员)中创建一个序列对象.

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

2) 将此代码添加到 id 变量:

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’是序列的名字,我创建了你可以随意命名,分配大小是按数字递增.

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) 将值插入数据库(教师 ID)时,发送 0 代替 Id,Hibernate 将自动从序列号转换该值.现在您可以使用这个数字来填充主题表.

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.

注意:在教师表中插入新对象时将返回 ID.使用该 ID,您可以填充主题表.有时最好用旧的方式来做,而不是使用 spring 已经提供的注释.

希望这能节省您的时间!

Hope this saves your time!

这篇关于在休眠中映射自动增量非主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆