在休眠状态下映射autoIncrement非主键 [英] Map autoIncrement non-primary key in hibernate

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

问题描述

我有一个教师表,其中包含一个主键TEACHER_UNIQUE_ID,另一个包含带有索引TEACHER_ID的autoIncrement键. 现在我必须将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.

我希望它插入带有实际自动递增的TEACHER_ID的Subject表.

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的Subject表."

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!

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

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