HIbernate子对象不保存

时间:2015-02-25 13:39:36

标签: java mysql hibernate

我有一个Employee和Employee部门表。一名员工可以拥有多个部门。

我在MySQL中定义了表并使用JPA生成了实体。

package model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;


/**
 * The persistent class for the emp1000 database table.
 * 
 */
@Entity
@NamedQuery(name="Emp1000.findAll", query="SELECT e FROM Emp1000 e")
public class Emp1000 implements Serializable {
    private static final long serialVersionUID = 1L;

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

    private String firstName;

    private String lastName;

    //bi-directional many-to-one association to EmpDept
    @OneToMany(mappedBy="emp1000")
    private List<EmpDept> empDepts;

    public Emp1000() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<EmpDept> getEmpDepts() {
        return this.empDepts;
    }

    public void setEmpDepts(List<EmpDept> empDepts) {
        this.empDepts = empDepts;
    }

    public EmpDept addEmpDept(EmpDept empDept) {
        getEmpDepts().add(empDept);
        empDept.setEmp1000(this);

        return empDept;
    }

    public EmpDept removeEmpDept(EmpDept empDept) {
        getEmpDepts().remove(empDept);
        empDept.setEmp1000(null);

        return empDept;
    }

}



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

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

    @Column(name="emp_dept")
    private String empDept;

    //bi-directional many-to-one association to Emp1000
    @ManyToOne
    @JoinColumn(name="emp_id")
    private Emp1000 emp1000;

    public EmpDept() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmpDept() {
        return this.empDept;
    }

    public void setEmpDept(String empDept) {
        this.empDept = empDept;
    }

    public Emp1000 getEmp1000() {
        return this.emp1000;
    }

    public void setEmp1000(Emp1000 emp1000) {
        this.emp1000 = emp1000;
    }

}

当我尝试生成创建Employee对象和关联的dept对象并将其保存到数据库时,子表永远不会被保存。

public class StoreData {
    public static void main(String[] args) {
        Session session=new AnnotationConfiguration()  
        .configure().buildSessionFactory().openSession();  

        //creating transaction object  
        Transaction t=session.beginTransaction();  


        Emp1000 e1 = new Emp1000();
        e1.setFirstName("Prem");
        e1.setLastName("Anand");

        EmpDept d1 = new EmpDept();
        d1.setEmpDept("Maths");
        d1.setEmp1000(e1);

        EmpDept d2 = new EmpDept();
        d1.setEmpDept("Science");
        d1.setEmp1000(e1);

        ArrayList<EmpDept> deptlist = new ArrayList();
        deptlist.add(d1);
        deptlist.add(d2);


        e1.setEmpDepts(deptlist);

        //session.saveOrUpdate(e1);

        session.persist(e1);//persisting the object  


        t.commit();//transaction is committed  

        session.close();  

        System.out.println("successfully saved");  


    }

    }

Employee对象是在表中创建的,而不是Employee dept对象。我需要更改哪些设置?

添加后出现新错误 - @OneToMany(mappedBy =&#34; emp1000&#34;,cascade = CascadeType.ALL)

Feb 25, 2015 8:43:43 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Field 'id' doesn't have a default value
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [model.EmpDept]
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)

2 个答案:

答案 0 :(得分:5)

您需要将持久操作级联到子实体。将empDept映射更改为

@OneToMany(mappedBy="emp1000", cascade = CascadeType.ALL)

答案 1 :(得分:1)

使用

@Cascade({org.hibernate.annotations.CascadeType.ALL}) 

而不是使用javax.persistence.CascadeType.ALL,如果我们同时使用hibernate和jpa,则会出现问题。