Reputation: 1624
i am trying to save data into hibernate but not able to save because of the error:
12:39:24,006 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-4) Unknown column 'department_id' in 'field list'
Please find the problem statement below:
This is my entity class :
@Entity
@Table(name = "employee")
@Where(clause = "isDeleted = false")
public class Employee extends BaseEntity {
@OneToOne
private Department department;
private int status;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Employee Table:
id | department | status
I am inserting data like this:
@Inject
CrudService<Employee> employeeManager;
Department department = new Department();
// create department and after creating depaerment
department.setDepartment(department);
department.setStatus(1);
department.setId(null);
employeeManager.create(department);
i am getting the error while persisting the data:
insert into employee (isdeleted, last_modification, department_id, status) values (?, ?, ?, ?)
here i can see that my Employee Table conatins department column but hibernate is generating department_id
internally to insert the records.
Can anybody help me? Please revert if you need any more information.
Upvotes: 0
Views: 223
Reputation: 691625
First, I guess many employees belong to the same department, so it should be a ManyToOne. Second, you can use JoinColumn to specify the name of the ... join column:
@ManyToOne
@JoinColumn(name = "department")
private Department department;
But I'd rather change the name of the column in the database, as department_id
makes it clearer that the column contains the ID of a department and is thus a foreign key.
Upvotes: 1