在保存实体时,多对一的外键存储为null

时间:2013-07-03 10:19:07

标签: spring hibernate

我是hibernate的新手。保存一个实体(EmployeeFamilyForm),它有两个多对一变量,但是一个多对一变量存储为null。另一个,多个到一个变量被正确存储。我在插入之前检查了实体,它有价值。

NO1:值作为空实体插入

@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:正确插入值实体

@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:我正在使用会话对象保存此实体,而employeeId存储为null。为什么没有存储价值?

@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;
}

在使用会话保存之前,这是具有参数名称和值的实体。

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 名称:qwe relationId:com.hrm.form.admin.RelationshipForm@4065c4

这是正确保存的参数和参数名称以及实体值。 class:class com.hrm.form.admin.RelationshipForm id:1 relName:null

这是保存为NULL参数和参数名称以及实体值。 class:class com.hrm.form.employee.EmployeeInfoForm designationId:null empDoj:null empId:dgfh empImage:null empName:null id:0 jobTitleId:null

这是hibernate的日志。为什么这个保存为NULL ??? Hibernate:插入employee_family(Dob,empid,name,relationid)值(?,?,?,?) 20:44:34,994 TRACE TimestampType:151 - 将'2013-07-11 00:00:00'绑定到参数:1 20:44:34,994 TRACE StringType:144 - 将 null 绑定到参数:2 20:44:34,994 TRACE StringType:151 - 将'qwe'绑定到参数:3 20:44:34,994 TRACE IntegerType:151 - 将'1'绑定到参数:4

0 个答案:

没有答案