Hibernate mappings
- From: JScoobyCed <pim@xxxxxxxx>
- Date: Tue, 06 Jun 2006 20:05:14 +0700
Hi,
I am facing a decision issue with Hibernate.
To represent a one-to-many relation, it seems there are two ways to do it with hibernate. Lets take a very simple example (it seems Employee/Department is widely used in Hibernate examples)
Employee: employeeId, employeeName
Department: departmentId, departmentName
EmployeeDepartment: employeeId, departmentId
An Employee is in one department only.
Solution 1:
<hibernate-mapping>
<class name="Employee" table="Employee">
<id name="employeeId" type="integer">
<column name="employeeId" sql-type="integer" not-null="true" />
<generator class="identity" />
</id>
<property name="employeeName" type="string">
<column name="employeeName" sql-type="char(32)" length="32"/>
</property>
</class>
<class name="Department" table="Department">
<id name="departmentId" type="integer">
<column name="departmentId" sql-type="integer" not-null="true" />
<generator class="identity" />
</id>
<property name="departmentName" type="string">
<column name="departmentName" sql-type="char(32)" length="32"/>
</property>
<set name="employees" table="EmployeeDepartment">
<key column="departmentId"/>
<one-to-many column="employeeId" class="Employee"/>
</set>
</class>
</hibernate-mapping>
Solution 2:
<hibernate-mapping>
<class name="Employee" table="Employee">
<id name="employeeId" type="integer">
<column name="employeeId" sql-type="integer" not-null="true" />
<generator class="identity" />
</id>
<property name="employeeName" type="string">
<column name="employeeName" sql-type="char(32)" length="32"/>
</property>
</class>
<class name="Department" table="Department">
<id name="departmentId" type="integer">
<column name="departmentId" sql-type="integer" not-null="true" />
<generator class="identity" />
</id>
<property name="departmentName" type="string">
<column name="departmentName" sql-type="char(32)" length="32"/>
</property>
</class>
<class name="EmployeeDepartment" table="EmployeeDepartment">
<composite-id name="id" class="EmployeeDepartmentId">
<key-property name="employeeId" type="int">
<column name="employeeId" />
</key-property>
<key-property name="departmentId" type="int">
<column name="departmentId" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
I am used to Solution 1. It is then easy to get a List of Employees from the Department class.
<code>
Session session = getSession();
Criteria crit = session.createCriteria(Department.class);
crit = crit.createCriteria("employees");
crit = crit.add(Restrictions.idEq(depId));
List list = crit.list();
</code>
What is not clear in Solution 2, is how can I get the List of Employee for a department. It seems I need to create an EmployeeDepartmentId object to get one Employee for a given group... Or am I missing something ?
And the question is: why would I select solution 1 rather than solutioin 2?
Thanks a lot for helping.
--
JSC
.
- Follow-Ups:
- Re: Hibernate mappings
- From: baratjan
- Re: Hibernate mappings
- Prev by Date: Prepared Statement
- Next by Date: Re: Hibernate mappings
- Previous by thread: Prepared Statement
- Next by thread: Re: Hibernate mappings
- Index(es):
Relevant Pages
|