-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many to many mapping
PostPosted: Sun Apr 09, 2006 8:07 am 
Newbie

Joined: Sun Apr 09, 2006 7:27 am
Posts: 3
Location: Zürich, Switzerland
Hibernate version:
3.1

Name and version of the database you are using:
mySQL 5.0 - INNODB

Hey, I need to have a sophisticated many-to-many mapping (or so I think). The problem is kind of hard to explain so I have made a quick muck up of the tables I would use if I were to make it without Hibernate.

Code:
CREATE TABLE `Class` (
  `id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
);

CREATE TABLE `Person` (
  `id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
);

CREATE TABLE `PersonClass` (
  `userId` int(11) NOT NULL,
  `classId` int(11) NOT NULL,
  `status` enum('pending','user','admin') NOT NULL,
  `type` enum('student','teacher') NOT NULL,
  PRIMARY KEY  (`userId`,`classId`),
  KEY `classId` (`classId`)
);

So I have a Class which can have a number of Persons associated. A Person is either student or teacher. And a Person is also either pending, user or admin. A Person can be associated with many Classes.

In my "Class" class I would like to have a pending, users (including both users and admins) and a admin Set. I would also like to have a students and teachers Set containing only user or admin Persons.

It is pretty complicated and I could make the mapping via many small many-to-many relations, but then a Person could end up in both the Teacher and Student Set, or worse be both pending and admin. can this kind of mapping be made in Hibernate without having PersonClass as a java class?

By the way comments on my table structure is welcome as long as it does what I have tried to sketch above ...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 8:43 pm 
Beginner
Beginner

Joined: Mon May 09, 2005 5:26 pm
Posts: 21
Hello,

can you post what you envision the object-land structure to look like? I only semi-understand what you're trying to do and that would make it much clearer.

-don.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 3:39 am 
Newbie

Joined: Sun Apr 09, 2006 7:27 am
Posts: 3
Location: Zürich, Switzerland
Well to make it easy the following functionality in object land would be sufficient:

Code:
// Class is a really bad name for a class I know but I used it in the first example
class "Class" {
  Set<Person> admins; // All associated Persons with admin in status field
  Set<Person> users; // All associated Persons with user in status field
  Set<Person> pendings; // All associated Persons with pending in status field
 
  Set<Person> students; // All associated Persons with student in type field
  Set<Person> teachers; // All associated Persons with student in type field
}


The above is slightly different from the first sketched idea. A person can for example be in the students Set and be in pending at the same time (this hopefully makes it easier to understand).

I am not sure that Hibernate can solve this (at least not elegant), so I am considering an Membership class which stores the information from the PersonClass table instead of relying on Hibernate to make this in between PersonClass/Membership table. Maybe it is another proof of the fact that I should not make up ideas while being partly drunk ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 2:23 pm 
Newbie

Joined: Fri Jul 15, 2005 10:02 am
Posts: 12
You can try using a custom SQL collection loader
This is just a sample, you would need to adapt it to your use.

Code:
<class>
<set name="permissionSet">
   <key />
   <many-to-many class="Permission" />
   <loader query-ref="permissionSet" />
</set>
etc....
</class>

<sql-query name="permissionSet">
     <load-collection alias="perms" role="Article.permissionSet" />
     SELECT perms.*
     FROM PERMISSIONS perms
     WHERE perms.TYPE = 'Y'
</sql-query>



check out this link

http://www.hibernate.org/hib_docs/v3/reference/en/html/querysql.html#querysql-load


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