Hello,
can anyone explain to me how one might implement the classic
"Person/Friend" problem using Hibernate?
in relational land a "Person" table has an ID and some info and a
"Friend" table is a self-join of the "Person" table -- that is, the
"Friend" table has two colums holding the ID's of two persons.
in object land, a class Person has a field "friends" of type Set, into
which are placed objects of type Person.
I'm *far* from a Hibernate wiz, but a person on our development team
who is using Hibernate claims it can't be done -- I find this difficult
to believe.
Bonus question: anyone know how to do the above using the new
java/hibernate annotations?
thanks much,
-don.
rpnman - 15 May 2005 15:19 GMT
I'm far from a Hibernate wiz myself, but have been learning it pretty
intensively over the last few weeks, and I think your intuitions are
on-target.
The case of type-self-referential relationships is covered in Bauer & King's
"Hibernate in Action", p 184. There the discussion is about parent/child
relationships where the relationship is bi-directional and symmetrical and
is many-to-one. If Category B refers to Category A as parent, then A had
better have B in its Set of child categories. What it has in common with
what you are asking about is the fact that one side of the relationship
(parent) maps a Set of objects of the same Type as the parent.
Following their model, part of your Person mapping might be something like:
<set name="friends">
<key column="PERSON_ID" />
<one-to-many class="Person" />
</set>
Beyond that you don't need to concern yourself with the generated table
structure, unless you want to build more properties on the friend
relationship itself. Hibernate will probably build a table much like the one
you have described.
A key question is whether the friend relation is symmetric. You seem to
imply it is not, but you haven't stated it clearly. If friends is a Set of
other Person objects, then there's nothing at the DB level or object level
to say that if A is in B's "friends" collection the complimentary inclusion
must also be true. If you have application logic in place to force symmetry,
then there will be to entries in the table, A friendof B and B friendof A.
If more attributes are associated, making friendship a separate class, which
contains a Set of Person objects may make sense. That also opens up the
possibility of many-person friendship associations.
Roger
roger.neyman@pobox.com
> Hello,
>
[quoted text clipped - 17 lines]
>
> -don.
rpnman - 15 May 2005 15:24 GMT
I'm far from a Hibernate wiz myself, but have been learning it pretty
intensively over the last few weeks, and I think your intuitions are
on-target.
The case of type-self-referential relationships is covered in Bauer & King's
"Hibernate in Action", p 184. There the discussion is about parent/child
relationships where the relationship is bi-directional and symmetrical and
is many-to-one. If Category B refers to Category A as parent, then A had
better have B in its Set of child categories. What it has in common with
what you are asking about is the fact that one side of the relationship
(parent) maps a Set of objects of the same Type as the parent.
Following their model, part of your Person mapping might be something like:
<set name="friends">
<key column="PERSON_ID" />
<one-to-many class="Person" />
</set>
Beyond that you don't need to concern yourself with the generated table
structure, unless you want to build more properties on the friend
relationship itself. Hibernate will probably build a table much like the one
you have described.
A key question is whether the friend relation is symmetric. You seem to
imply it is not, but you haven't stated it clearly. If friends is a Set of
other Person objects, then there's nothing at the DB level or object level
to say that if A is in B's "friends" collection the complimentary inclusion
must also be true. If you have application logic in place to force symmetry,
then there will be two entries in the table, corresponding to the predicates
"A friendof B" and "B friendof A".
If more attributes are associated, making friendship a separate class, which
contains a Set of Person objects may make sense. That also opens up the
possibility of many-person friendship associations.
Roger
roger.neyman@pobox.com
> Hello,
>
[quoted text clipped - 17 lines]
>
> -don.