1

我是 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() {
            }

这是我的实体类,实体有一个 getter 和一个 setter

4

2 回答 2

0

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

同样的错误发生在Designation课堂上。

参见Primitive or wrapper for hibernate primary keys

于 2013-09-18T13:03:30.840 回答
0

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

@OneToMany(mappedBy="designation")//, cascade=CascadeType.ALL
private List<Employee> employees = new LinkedList<>();
于 2013-09-18T12:57:29.343 回答