-->
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: I thought I knew the basics of Hibernate...
PostPosted: Mon Dec 08, 2003 9:51 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 7:21 am
Posts: 27
Location: Sweden
...Well, that's what I thought - but today I stumbled into a problem that *seems* so trivial that I'm embarrassed to ask for a solution. In short: if I want don't want to map a many-to-many relation as class-to-class, but rather as "class-with-list"
(don't know what to call it..) - then how do I do that?

Here's the DB:

Code:
table user(
   integer user_id primary key,
   text user_name
);

table user_permission(
   integer user_id,
   text per_name,
   primary key (user_id, per_name),
   foreign key (user_id) references user,
   foreign key (per_name) references permissions
);

table permissions(
   text per_name primary key
);


Now, I don't want to map this as

class User <--m-t-m--> class Permission

..as it would result in having to do..

Code:
User user = ..
Set permissions = user.getPermissions();
for (Iterator i = permissions.iterator(); i.hasNext(); ){
   Permission p = p.next();
   if (p.getName().equals("order_delete")){
      // add menu item "Delete this order"
   }
}


..instead of just..

Code:
if (user.getPermissions().contains("order_delete")){
   ...
}


..that is, if permissions was mapped to a java.util.List in the User class.

It's still a m-t-m relation in the DB though, but as many-to-many requires a class attribute (and there's no Permission class) I'm unsure
how to map this.

TIA

- Mikael.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 11:54 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
You may model your association as one-to-many or many-to-many. The permission collection can be e.g. map, list, or set.
With map, you can use the method containsKey("order_delete").
With list or set, you need to implement equals() and hashCode() methods for Permission, and use contains(new Permission("order_delete"));

mota


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 8:00 am 
Beginner
Beginner

Joined: Fri Sep 05, 2003 7:21 am
Posts: 27
Location: Sweden
mota wrote:
You may model your association as one-to-many or many-to-many. The permission collection can be e.g. map, list, or set.
With map, you can use the method containsKey("order_delete").
With list or set, you need to implement equals() and hashCode() methods for Permission, and use contains(new Permission("order_delete"));

mota


I think you've missed a point - there is no (and shouldn't be any) Permission class. Thus, I can't map this as many-to-many (which it is, relational wise), as
Code:
<many-to-many>
requires a
Code:
class
attribute. There's the problem - ie, I don't want to load entries in the permissions table into instances of a Permission class, but rather into a List of Strings in the User class.

Ie,

Code:
class User {

   List permissions; // List of String values - "order_add", "order_update", etc
   ...

   public void setPermissions(List permissions);
   public List getPermissions();

   ...

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 9:59 am 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Read Hibernate documentation. You can have a collection of objects corresponding to entire rows in the associated table (e.g. class Permission), subset of the row (few columns from permission table), or one field (e.g. list of strings).

mota


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.