子级对象未保存 [英] HIbernate child object not saving

查看:100
本文介绍了子级对象未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Employee和Employee Dept表.一个员工可以有多个部门.

I have a Employee and Employee Dept table. One employee can have multiple departments.

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

I have defined the tables in MySQL and generated the entities using 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对象并将其保存到数据库时,子表永远不会被保存.

When I try to generate to create a Employee object and associated dept objects and save it to database, the child table never gets saved.

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对象中创建.我需要更改哪些设置?

The Employee object is created in the table but not the Employee dept objects. What settings do I need to change?

添加后的新错误-@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映射更改为

You need to cascade the persist operation down to child entities. Change empDept mapping to

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

这篇关于子级对象未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆