Spring和Hibernate Error - not-null属性引用null或transient值:com.tharaka.model.Employee.designation

时间:2013-09-18 12:23:01

标签: spring hibernate spring-mvc spring-security

我是Spring和hibernate的新手,我在尝试保留事务数据时遇到了上述错误。请尝试帮助解决这个问题 这是我的实体:

 @Entity @NamedQuery(name="Employee.findAll", query="SELECT e FROM    Employee e") 

    public class Employee implements Serializable{      

    private    static final long serialVersionUID = 1L;     
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)   
    private int id;
    private String city;    
    private String civil;
    @Temporal(TemporalType.DATE)    
    @Column(name="dob", length=11)
    private Date dob;   
    private String email;   
    private int epf;    
    private String fname;   
    private String gender;  
    private int landtp;     
    private String lname;   
    @Temporal(TemporalType.DATE)
    @Column(name="salaryincrement", length=11)  
   //bi-directional many-to-one association to    Designation   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name="designation_id", nullable=false)   
   private    Designation designation;  

   public Employee() {  }



    @Entity
    @NamedQuery(name="Designation.findAll", query="SELECT d FROM Designation d")
    public class Designation implements Serializable{ 
      private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String type;

    //bi-directional many-to-one association to Employee
    @OneToMany(mappedBy="designation")//, cascade=CascadeType.ALL
    private List<Employee> employees;

    public Designation() {
            }

这是我的Entity类, 实体有一个getters ans setters

2 个答案:

答案 0 :(得分:0)

designation设置为nullable = false。但employees变量未在Designation中初始化。所以,你必须初始化为

@OneToMany(mappedBy="designation")//, cascade=CascadeType.ALL
private List<Employee> employees = new LinkedList<>();

答案 1 :(得分:0)

我不确定您是否可以使用基本类型int作为您的ID - 您应该使用Integer - 因为int具有默认的零值且不能 null ,您的新记录可以被视为具有Id ZERO的分离实体,而不是瞬态实体。

同样的错误发生在Designation类。

请参阅Primitive or wrapper for hibernate primary keys