samedi 1 août 2015

How to use List in Hibernate XML mapping?

I have Department and Employee entities, I want to map them as one to many. How can I use List in XML mapping instead of Set? I found this code from Internet, but when execute "from Department", the error is "Repeat property department_id in Employee.department", but if I remove the <index> in department, it's will be "Unable to read XML". Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://ift.tt/IMLJSW">
<!-- Generated Aug 2, 2015 7:14:52 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="emp.entity.Department" table="department" schema="dbo" catalog="Emp" optimistic-lock="version">
        <id name="departmentId" type="long">
            <column name="department_id" />
            <generator class="assigned" />
        </id>
        <property name="deptName" type="string">
            <column name="dept_name" length="50" not-null="true" />
        </property>
        <list name="employees" table="Employee" inverse="true" cascade="all" >
            <key>
                <column name="department_id" />
            </key>
            <index column="department_id"></index>
            <one-to-many class="emp.entity.Employee" />
        </list>
    </class>
</hibernate-mapping>

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://ift.tt/IMLJSW">
<!-- Generated Aug 2, 2015 7:14:52 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="emp.entity.Employee" table="Employee" schema="dbo" catalog="Emp" optimistic-lock="version">
        <id name="employeeId" type="int">
            <column name="employee_id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="department"  class="emp.entity.Department" fetch="select">
            <column name="department_id" />
        </many-to-one>
        <property name="firstname" type="serializable">
            <column name="firstname" />
        </property>
    </class>
</hibernate-mapping>

Aucun commentaire:

Enregistrer un commentaire