无法通过hibernate一对多双向进程中的父表将数据存储到子表中

时间:2012-10-11 07:16:55

标签: hibernate

我试图通过DEPARTMENT(父)表将数据存储到EMPLOYEE(子)表中,并使用hibernate一对多的双向过程显示它。

在交易之后,我能够透露员工中的数据,包括我想要输入的数据,但我无法在数据库中看到该数据(POSTGRES)。请帮助我们,并提前致谢。

我有两个表部门(父母)和员工(子部门负责部门的dep_id)。

CREATE TABLE department
(
department_id character varying(10) NOT NULL,
department_name character varying(20),
CONSTRAINT "pk_departmentId" PRIMARY KEY (department_id)
) 

CREATE TABLE employee
(
emp_id character varying(10) NOT NULL,
emp_name character varying(20),
department_id character varying(10),
CONSTRAINT "pk_empId" PRIMARY KEY (emp_id),
CONSTRAINT "fk_empDept" FOREIGN KEY (department_id) REFERENCES department (department_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
)

各个hbm文件是:

* DEPARTMENT *

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="tablebeans.Department" table="department" schema="public">
        <id name="departmentId" type="string">
            <column name="department_id" length="10" />
            <generator class="assigned" />
        </id>
        <property name="departmentName" type="string">
            <column name="department_name" length="20" />
        </property>
        <set name="employees" table="employee" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="department_id" length="10" />
            </key>
            <one-to-many class="tablebeans.Employee" />
        </set>
    </class>
</hibernate-mapping>

*的 EMPLOYEE *

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="tablebeans.Employee" table="employee" schema="public">
        <id name="empId" type="string">
            <column name="emp_id" length="10" />
            <generator class="assigned" />
        </id>
        <many-to-one name="department" class="tablebeans.Department" fetch="select">
            <column name="department_id" length="10" />
        </many-to-one>
        <property name="empName" type="string">
            <column name="emp_name" length="20" />
        </property>
    </class>
</hibernate-mapping>

POJO课程是:

package tablebeans;
// Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA




import java.util.HashSet;
import java.util.Set;

/**
 * Department generated by hbm2java
 */
public class Department  implements java.io.Serializable {

     private String departmentId;
     private String departmentName;
     private Set employees = new HashSet(0);

    public Department() {
    }

    public Department(String departmentId) {
        this.departmentId = departmentId;
    }
    public Department(String departmentId, String departmentName, Set employees) {
       this.departmentId = departmentId;
       this.departmentName = departmentName;
       this.employees = employees;
    }

    public String getDepartmentId() {
        return this.departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }
    public String getDepartmentName() {
        return this.departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    public Set getEmployees() {
        return this.employees;
    }

    public void setEmployees(Set employees) {
        this.employees = employees;
    }
}


package tablebeans;
// Generated Oct 9, 2012 3:01:23 PM by Hibernate Tools 3.2.1.GA





/**
 * Employee generated by hbm2java
 */
public class Employee  implements java.io.Serializable {

     private String empId;
     private Department department;
     private String empName;

    public Employee() {
    }

    public Employee(String empId) {
        this.empId = empId;
    }
    public Employee(String empId, Department department, String empName) {
       this.empId = empId;
       this.department = department;
       this.empName = empName;
    }

    public String getEmpId() {
        return this.empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }
    public Department getDepartment() {
        return this.department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
    public String getEmpName() {
        return this.empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }
}

我试图通过DEPARTMENT表将数据存储到EMPLOYEE表并显示它的代码。

 tx = session.beginTransaction();

            Department department=new Department();
            department.setDepartmentId("5");
            department.setDepartmentName("Nielson");

            Employee employee = new Employee();
            employee.setEmpId("4");
            employee.setEmpName("phani");
            employee.setDepartment(department);

            Set empset = new HashSet();
            empset.add(employee);
            department.setEmployees(empset);

            session.save(department);
            tx.commit();

            //Query query = session.createQuery("insert into Department(departmentId,departmentName)"+"select departmentId,departmentName from Employee where departmentId = 5");
            //int update = query.executeUpdate();

            Query query1=session.createQuery("from Department");
            List list1=query1.list();
            for(int i=0;i<list1.size();i++)
               {
                System.out.println("In for");
                Department department1 =(Department) list1.get(i);
                System.out.println("the ID is "+department1.getDepartmentId());
                System.out.println("the dept name is "+department1.getDepartmentName());
                Set st = (Set) department1.getEmployees();
                Iterator ite = st.iterator();
                while(ite.hasNext())
                {
                    employee = (Employee) ite.next();
                    System.out.println("the emp name is "+employee.getEmpName());
                }

              }

在这里,我能够将新输入的记录显示在employee表中,但该数据与数据库表中的数据不相似。我正在使用Postgres。

1 个答案:

答案 0 :(得分:1)

仅在地图文件中添加 cascade =“save-update”。 对于部门来说,

<class name="tablebeans.Department" table="department" schema="public">
        <id name="departmentId" type="string">
            <column name="department_id" length="10" />
            <generator class="assigned" />
        </id>
        <property name="departmentName" type="string">
            <column name="department_name" length="20" />
        </property>
        <set name="employees" table="employee" inverse="true" lazy="true" fetch="select" cascade="save-update">
            <key>
                <column name="department_id" length="10" />
            </key>
            <one-to-many class="tablebeans.Employee" />
        </set>
    </class>

您可以在

中看到这一点

Cascade example