I'm new to hibernate. While save an entity(EmployeeFamilyForm) which has two many to one variable,but one many to one variable stored as null.The another, many to one variable is stored correctly. I've checked the entity before insertion, it has value.
NO1: value inserted as null entityCode:
@Entity()
@Table(name = "employee_info")
public class EmployeeInfoForm implements Serializable {
@Id @GeneratedValue()
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "empid", unique = true, length = 10, nullable = false)
private String empId;
@Column(name = "empname", length = 25, nullable = false)
private String empName;
@NotNull(message="*")
@DateTimeFormat(pattern="dd/MM/YYYY")
@Column(name = "doj", nullable = false)
private Date empDoj;
@ManyToOne
@JoinColumn(name = "designation", referencedColumnName = "id", insertable = true, updatable = true)
private EmployeeDesignationForm designationId;
@ManyToOne
@JoinColumn(name = "jobtitle", referencedColumnName = "id", insertable = true, updatable = true)
private EmployeeDesignationForm jobTitleId;
@Column(name = "employee_image")
private Blob empImage;
}
NO2: Value inserted entity correctlyCode:
@Entity()
@Table(name = "admin_relationship")
public class RelationshipForm {
@Id @GeneratedValue
@Column(name = "id", nullable = false)
private int id;
@Column(name = "relationship", length = 25, nullable = false)
@Size(max = 25, message = "*invalid")
@NotEmpty(message = "*")
private String relName;
}
NO3: I'm saving this entity using session object and employeeId stored as null. Why the value is not stored?Code:
@Entity()
@Table(name = "employee_family")
public class EmployeeFamilyForm {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@ManyToOne()
@JoinColumn(name = "empid", referencedColumnName = "empId")
private EmployeeInfoForm employeeId;
@Column(name = "name", length = 25, nullable = false)
@Size(max = 25, message = "*invalid")
@NotEmpty(message = "*")
private String name;
@ManyToOne
@JoinColumn(name = "relationid", referencedColumnName = "id")
private RelationshipForm relationId;
@DateTimeFormat(pattern = "dd/MM/yyyy")
@NotNull(message = "*")
private Date Dob;
}
This is the entity with parameter name and value before saving using session.
class : class com.hrm.form.employee.EmployeeFamilyForm dob : Thu Jul 11 00:00:00 IST 2013 employeeId : com.hrm.form.employee.EmployeeInfoForm@c3bb57 id : 0 name : qwe relationId : com.hrm.form.admin.RelationshipForm@4065c4
This is the correctly saving parameter and parameter name and value of entity.
class : class com.hrm.form.admin.RelationshipForm id : 1 relName : null
This is the saving as NULL parameter and parameter name and value of entity.
class : class com.hrm.form.employee.EmployeeInfoForm designationId : null empDoj : null empId : dgfh empImage : null empName : null id : 0 jobTitleId : null
This is the log of hibernate.
Why is this saving as NULL??? Hibernate: insert into employee_family (Dob, empid, name, relationid) values (?, ?, ?, ?) 20:44:34,994 TRACE TimestampType:151 - binding '2013-07-11 00:00:00' to parameter: 1 20:44:34,994 TRACE StringType:144 - binding null to parameter: 2 20:44:34,994 TRACE StringType:151 - binding 'qwe' to parameter: 3 20:44:34,994 TRACE IntegerType:151 - binding '1' to parameter: 4