Hibernate mappings



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
.



Relevant Pages

  • PLEASE HELP! Tomcat and Hibernate ServletException, net/sf/cglib/core/KeyFactory problem
    ... page to configure Tomcat to work with Hibernate. ... I tested hibernate with a simple Employee table and Employee.hbm.xml ... INFO: Optimize cache for minimal puts: false ...
    (comp.lang.java.databases)
  • Re: Hibernate: how to retreive only few properties in a class
    ... Note the "getEmployeesSummaryList" and the return employee clause. ... I get a java.sql.SQLException: Column 'LoginName' not ... My guess is that Hibernate tries to map every column ... Hibernate to map only the columns found in the ResultSet? ...
    (comp.lang.java.databases)
  • Hibernate und unidirektionales ManyToOne-Mapping
    ... Hibernate ziemliche Probleme beim Löschen bereitet? ... private String name; ... Datenbank-technisch legt Hibernate jetzt ja eine Tabelle "Employee" an ...
    (de.comp.lang.java)
  • Re: Newbie Needs Help !!!
    ... Session session = HibernateSessionFactory.getSession; ... Employee Employee = new Employee; ... // Step 4 - Get a Hibernate Session ... You are not committing the transaction. ...
    (comp.lang.java.databases)
  • Newbie Needs Help !!!
    ... Hello I'm trying to Create a Simple write to an oracle Database with ... Session session = HibernateSessionFactory.getSession; ... Employee Employee = new Employee; ... // Step 4 - Get a Hibernate Session ...
    (comp.lang.java.databases)