Reputation: 131
I am practice Hibernate with the following classes and a MySQL database.
@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;
}
}
I keep getting the following error message from these sql statements:
Hibernate: insert into student_address (address_detail) values (?)
Hibernate: insert into Student (address_address_id, student_name) values (?, ?)
Error Message:
Unknown column '**address_address_id'** in 'field list'
My database has the field name address_id
.
Hibernate keeps appending address
to address_id
and changes the column name. I could probably change the field name from address
to address_address_id
in my database but what is causing this to happen. Is it a valid behavior in Hibernate and can I change it?
Upvotes: 1
Views: 1384
Reputation: 23562
That's the default column name Hibernate uses for @ManyToOne
association:
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.
To specify the desired column name:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address")
private StudentAddress address;
Upvotes: 2