-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to map a one-to-many relationship to the same entity
PostPosted: Wed Sep 12, 2007 10:24 am 
Newbie

Joined: Wed Sep 12, 2007 10:02 am
Posts: 1
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


Top
 Profile  
 
 Post subject: I have same issue.. did you solve it?
PostPosted: Mon Apr 21, 2008 2:22 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
I have same issue.. did you solve it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.