簡體   English   中英

子級對象未保存

[英]HIbernate child object not saving

我有一個Employee和Employee Dept表。 一名員工可以有多個部門。

我已經在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="emp1000", 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)

您需要將持久化操作級聯到子實體。 empDept映射更改為

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

采用

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

如果我們不同時使用hibernate和jpa,則不是使用javax.persistence.CascadeType.ALL,而是一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM