简体   繁体   English

在休眠状态下映射autoIncrement非主键

[英]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. 我有一个教师表,其中包含一个主键TEACHER_UNIQUE_ID,另一个包含带有索引TEACHER_ID的autoIncrement键。 Now i have to map autoIncrement key to other table ie SUBJECT . 现在我必须将autoIncrement键映射到其他表,即SUBJECT。 I have used below code, but this always sets TEACHER_ID as null in subject class. 我已经使用了以下代码,但是在主题类中,这始终将TEACHER_ID设置为null。

i want it to insert Subject table with actual autoIncremented TEACHER_ID. 我希望它插入带有实际autoIncremented TEACHER_ID的Subject表。

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. 除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." 解决此问题的方法:“我希望它插入带有实际autoIncremented TEACHER_ID的Subject表。”

I would suggest keeping a sequence object in the database to manually track the increment. 我建议在数据库中保留一个序列对象以手动跟踪增量。

To generate ID automatically using Hibernate: 要使用Hibernate自动生成ID,请执行以下操作:

1) Create a sequence object in your database developer(SQL developer). 1)在数据库开发人员(SQL开发人员)中创建一个序列对象。

2) Add this code to id variable: 2)将此代码添加到id变量中:

  @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. 3) 'ONE'是序列的名称,我已经创建了您可以随意命名的名称,分配大小是数字的增量。

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. 4)将值插入数据库(教师ID)时,发送0代替Id,Hibernate将自动转换序列号中的值。 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. 注意:在教师表中插入新对象时,将返回ID。 Using that ID you can populate the Subject table. 使用该ID可以填充“主题”表。 it is sometimes better to do it old way rather than using the already provided annotations by spring. 有时最好以旧方式进行操作,而不是使用spring之前已提供的注释。

Hope this saves your time! 希望这可以节省您的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过非主键休眠内部联接 - Hibernate Inner Join by non-primary key 如何 map 一对多结果到使用非主键连接的 Hibernate 实体? - How to map a one-to-many result to a Hibernate entity which is joined using a non-primary key? 在非主键列上使用Hibernate Criteria进行内部联接 - Inner joins using Hibernate Criteria on non-primary key column 将主键映射到另一个具有不同名称的表中的非主键 - Map primary key to non-primary key in another table with different name 休眠:@ManyToOne(fetch = FetchType.LAZY) 对非主键引用列不起作用 - Hibernate: @ManyToOne(fetch = FetchType.LAZY) does not work on non-primary key referenced column 带有外键的休眠表引用同一表的非主列不返回结果 - Hibernate table with foreign key referring to same table non-primary column returns no result 如何在非主键上使用休眠注释设置单向一对一关系? - how to set unidirectional one-to-one relationship using hibernate annotation on a non-primary key? 如何在 Hibernate 的复合主键中自动增加一个 Id? - How to autoincrement an Id in a composite primary key in Hibernate? 如何联接非主键列上的表? - How do I join tables on non-primary key columns? 为什么@ManyToMany无法与非主键列一起使用? - Why is @ManyToMany not working with non-primary key columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM