I am new to Hibernate world. It may be a silly question, but I am not able to solve it. I am testing many to One relationship of tables and trying to insert record. I have a
Mattias' answer is almost right, but ForeignKey is used for schema generation. Try the below mapping instead. Also, you have the Employees collection mapped with CascadeAll. This will delete employee records if you delete a department, which is probably not desirable.
public class DeptMapping : ClassMap
{
public DeptMapping()
{
Id(x => x.Id);
Map(x => x.DeptName);
Map(x => x.DeptLocation);
HasMany(x => x.Employees).KeyColumn("DeptId").Inverse().Cascade.All();
}
}
public class EmployeeMapping : ClassMap
{
public EmployeeMapping()
{
Id(x => x.Id);
Map(x => x.EmpName);
Map(x => x.EmpAge);
Map(x => x.DeptId);
References(x => x.Dept, "DeptId").Cascade.None();
}
}