我使用以下类和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中的有效行为吗?我可以更改吗?
答案 0 :(得分:1)
default column name Hibernate用于@ManyToOne
关联:
默认值(仅在使用单个连接列时适用): 连接以下内容:引用的名称 关系属性或引用实体的字段或可嵌入的 类; " _&#34 ;;引用的主键列的名称。如果有 在实体中没有这样的引用关系属性或字段,或 如果连接用于元素集合,则连接列名称为 形成如下的串联:实体的名称; " _&#34 ;;引用的主键列的名称。
指定所需的列名称:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address")
private StudentAddress address;