-->
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: how to describe the unusual many-to-many relation?
PostPosted: Wed Apr 20, 2005 11:15 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 3:48 am
Posts: 23
A user table mapping to a typesafe enum, a user can has more than one roles, and of course one role can assigned to more than one users.

for example:

the user table has 2 fields (login_id, pwd), so the hibernate class is like:

Code:
public class user{
    private String loginId;
    private String pwd;
    private Set roles = new HashSet();
}


the typesafe enum of the roles is like:
Code:
public class UserRole {
    private final String roleName;
    private UserRole(String roleName) {
        this.roleName = roleName;
    }
    public String toString() {
        return roleName;
    }
    public static final UserRole ROLE1 = new UserRole("role1");
    public static final UserRole ROLE2 = new UserRole("role2");
    public static final UserRole ROLE3 = new UserRole("role3");
}


and there's another table to save the relationships between the user and role, this table has 2 fields:(login_id, role_name);

so, how many classes I should write ? how many hbm.xml files I should write and how to write the hbm.xml files?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 3:53 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
You can have simple many-to-many, with no additional information in binding table --> two classes needed, each with its own mapping file.

Or you can have additional information in binding table - then there is suggested one-to-many from User and Role to UserRole, and two many-to-one in binding UserRole.

See 'Hibernate in Action' for more details.


Top
 Profile  
 
 Post subject: Unusual One-Tomany
PostPosted: Thu Apr 21, 2005 4:58 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Hi

You can use the middleGen tool and geenrate both the Class files and hbm.xml's from your DB. It will show the appropriate mappings also.


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.