Hi all,
I want to map Hibernate against one of our legacy database. I'm having trouble with one of the use case, I am not sure if Hibernate could handle this elegantly.
Here is my entity class:
Code:
public class Person
{
private int id;
private String name;
private String departmentName;
...
//get/set methods
[b]public Set<Person> getOtherPeopleInTheSameDepartment();[/b]
//this method should return a set of Person objects that has the same departmentName;
}
Here is my db schema:
Code:
Table: Person
Column:
id - int
name - varchar
department - varchar
...
I am trying to define this relation in my hibernate mapping file with a sql named query, but it doesnt work. I am not sure how can I define a one-to-many mapping that maps back to the same Class. I really appreciate your help!
Code:
<hibernate-mapping>
<class name="Person" table="Person" schema="dbo" catalog="Scrap">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="department" type="string">
<column name="department" not-null="true" />
<set name="otherPeopleInTheSameDepartment" inverse="true" lazy="true">
<key>
<column name="id" not-null="true" />
<column name="department" not-null="true" />
</key>
<one-to-many class="Person"/>
<loader query-ref="Person.otherPeopleInTheSameDepartment"/>
</set>
<sql-query name="otherPeopleInTheSameDepartment">
<load-collection alias="p" role="Person.otherPeopleInTheSameDepartment"/>
select
{p.*}
from
Person p
where
p.ID = :id
and p.department = :department
</sql-query>
</class>
</hibernate-mapping>
Hibernate version:
Hibernate 3.2
Name and version of the database you are using:
MSSQL