-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many mapping to same class
PostPosted: Thu Aug 18, 2005 6:33 am 
Newbie

Joined: Thu Aug 18, 2005 5:55 am
Posts: 2
Hi guys,

this is my first post on the forum so please forgive me if my question has already been answered. I've searched the forums, online doco etc, and still can't find a solution for what I'm trying to do.

I'm using hibernate 2.1, with mySql 4.01 and java1.4.2_08

I want to create a many-to-many mapping from a class to itself.

For example, I have a User class who submits documents for review. A User can both be a reviewer and a reviewee. So I would have something like the following:

Code:
public class User
{
  Set reviewers;
  Set reviewees;

  /* when I add a reviewer, I must become a reviewee for this user */
  public void addReviewer(User rr)
{
    rr.addReviewee(this);
    reviewers.add(rr);
}

/* when I add a reviewee, I must become a reviewer for them */
public void addReviewee(User re)
{
    re.addReviewer(this);
    reviewees.add(re);
}

/* return a set of users who reviewed my document */
Set getReviewers() { return reviewers; }

/* get a list of users who's documents I reviewed */
Set getReviewees() { return reviewees; }
}


I can't get mapping correct:
Code:
<class name="user" class="User" table="USER">
     <set role="reviewers"  table="USER_REVIEWS">
         <key column="reviewer_id"/>
        <many-to-many column="reviewee_id" class="User"/>
    </set>
    <set role="reviewees" table="USER_REVIEWS">
        <key column="reviewee_id"/>
        <many-to-many columm="reviewer_id" class="User"/>
    </set>
</class>


I'd expect to have two table in my database, USER table and a USER_REVIEWS table. The second table I'd expect to contain two columns, a reviewer_id and a reviewee_id that are both foreign keys back to the user table, with both columns making up the primary key.

I'd appreciate any help, even if its just a RTFM pointed at the right place ;-)
Thanks.
dbrady.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 2:49 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 12:03 pm
Posts: 48
Location: Kiel, Germany
What problem do you have with your mapping? Can you provide any error messages? When does the error occur?

I don't know if there is something like a 'role' attribute to the set element but AFAIK a 'name' attribute is mandatory, so perhaps it works with
Code:
<set name="reviewers"  table="USER_REVIEWS">


And there seems to be a spelling mistake in
Code:
<many-to-many columm="reviewer_id" class="User"/>


Generally the mapping looks ok.

_________________
Please don't forget to rate if you find this posting useful.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 12:38 am 
Newbie

Joined: Thu Aug 18, 2005 5:55 am
Posts: 2
I managed t get this working, so thought I'd post the code snippet for the benefit of others trying to do something similiar:

Code:
<class name="mypackage.User" table="USERS">
   <id name="id" column="userID_pk" type="int" unsaved-value="0">
         <meta attribute="scope-set">protected</meta>
         <generator class="increment" />
   </id>

   <set name="reviewers" table="USER_REVIEWERS" lazy="false">
      <meta attribute="field-description">Set of Reviewers for this User</meta>
      <key column="REVIEWEE_ID"/>
      <many-to-many column="REVIEWER_ID" class="mypackage.User" />
   </set>
         
   <set name="reviewees" table="USER_REVIEWERS" lazy="false">
      <key column="REVIEWER_ID"/>
      <many-to-many column="REVIEWEE_ID" class="mypackage.User"/>
   </set>


and in the User.java file you must be careful to avoid an infinite loop when adding reviewer/reviewee. The following works:

Code:

    public Set getReviewers()
    {
        return this.reviewers;
    }

    public void setReviewers(Set reviewers)
    {
        this.reviewers = reviewers;
    }

    public Set getReviewees()
    {
        return this.reviewees;
    }

    public void setReviewees(Set reviewees)
    {
        this.reviewees = reviewees;
    }

    public void addReviewer(User reviewer)
    {
        if (reviewers == null)
        {
            reviewers = new HashSet();
        }

        if (!reviewers.contains(reviewer))
        {
            reviewers.add(reviewer);
            reviewer.addReviewee(this);
        }
    }

    public void addReviewee(User reviewee)
    {

        if (reviewees == null)
        {
            reviewees = new HashSet();
        }

        if (!reviewees.contains(reviewee))
        {
            reviewees.add(reviewee);
            reviewee.addReviewer(this);
        }
    }


This results in a user_reviewer table with two columns reviewee_id and reviewer_id which combine to form the primary key and each refers back to the user table, one for the user who is the reviewer and the other who is the reviewee.

Cheers,
dbrady.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.