gpt4 book ai didi

java - Hibernate 更改我的字段名称

转载 作者:行者123 更新时间:2023-11-29 12:04:15 26 4
gpt4 key购买 nike

我正在使用以下类(class)和 MySQL 数据库练习 Hibernate。

@Entity
@Table(name="Student")
public class Student {

@Id
@GeneratedValue
private int student_id;

private String student_name;

@ManyToOne(cascade=CascadeType.ALL)
private StudentAddress address;

@Transient
@Temporal(TemporalType.DATE)
private Date birthDay;

public Student() {
}

public Date getBirthDay() {
return birthDay;
}

public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}

public int getStudent_id() {
return student_id;
}

public void setStudent_id(int rollNo) {
this.student_id = rollNo;
}

public String getStudent_name() {
return student_name;
}

public void setStudent_name(String name) {
this.student_name = name;
}

public StudentAddress getAddress() {
return address;
}

public void setAddress(StudentAddress address) {
this.address = address;
}
}

@Entity
@Table(name="student_address")
public class StudentAddress {

@Id
@GeneratedValue
private int address_id;

private String address_detail;

public int getAddress_id() {
return address_id;
}

public void setAddress_id(int address_id) {
this.address_id = address_id;
}

public String getAddress_detail() {
return address_detail;
}

public void setAddress_detail(String address_detail) {
this.address_detail = address_detail;
}
}

我不断从这些 sql 语句中收到以下错误消息:

Hibernate: insert into student_address (address_detail) values (?)
Hibernate: insert into Student (address_address_id, student_name) values (?, ?)

错误消息:

Unknown column '**address_address_id'** in 'field list'

我的数据库的字段名称为address_id

Hibernate 不断将 address 附加到 address_id 并更改列名称。我可能可以将数据库中的字段名称从 address 更改为 address_address_id 但是什么导致了这种情况发生。这是 Hibernate 中的有效行为吗?我可以更改它吗?

最佳答案

这就是default column name Hibernate 使用 @ManyToOne 关联:

Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "_"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; "_"; the name of the referenced primary key column.

指定所需的列名称:

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address")
private StudentAddress address;

关于java - Hibernate 更改我的字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31820620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com