0

我正在尝试通过 DEPARTMENT(parent) 表将数据存储到 EMPLOYEE(child) 表中,并使用休眠的一对多双向进程显示它。

交易完成后,我可以调整员工中的数据,包括我想要输入的数据,但我无法在数据库(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 文件是:

*部门*

<?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>

*员工*

<?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());
                }

              }

在这里,我可以将新输入的记录显示到员工表中,但该数据与数据库表中的数据不同。我正在使用 Postgres。

4

1 回答 1

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>

你可以看到这个

级联示例

于 2012-10-11T07:53:08.393 回答