0

DEPARTMENT(親)テーブルを介してEMPLOYEE(子)テーブルにデータを格納し、休止状態の1対多の双方向プロセスを使用してデータを表示しようとしています。

トランザクション後、入力したいデータを含む従業員のデータをダイヤルできますが、データベース(POSTGRES)でそのデータを表示できません。よろしくお願いします。

Department(Parent)とemployee(departmentでdep_idを参照する子)の2つのテーブルが必要です。

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

              }

ここでは、新しく入力されたレコードをemployeeテーブルに表示できますが、そのデータはデータベーステーブルに似ていません。私は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 に答える