Hello to all appfuse users,

I have a doubt.

I have a created two entities called User and Role. A user can have many
roles and a role can have many users.


User.java

    code:


    @Entity
    @Table(name="app_user")
    public class User {
     
    .....
    private Set<Role> roles = new HashSet<Role>();
    .......
     
    @ManyToMany(fetch = FetchType.EAGER, targetEntity=Role.class, cascade =
CascadeType.ALL)
        @JoinTable(
                name="user_role",
                joinColumns = {
                         @JoinColumn(name = "username", referencedColumnName
= "username")
                               },
                inverseJoinColumns = {
                         @JoinColumn(name = "role_name",
referencedColumnName = "name")
                                     }
        )
        public Set<Role> getRoles() {
            return roles;
        }
    ......
    ......
    .......
    }
     






Role.java

    code:


    @Entity
    @Table(name="role")
    public class Role{
     
    .....
     private Set<User> users;
    ..
    ...
     @ManyToMany(mappedBy="roles", targetEntity=User.class)
        public Set<User> getUsers() {
            return users;
        }
     
    ....
    .........
    ......
    }
     



Now I got three tables. ie app_user (User) table, role table and user_role
table.

    code:


     
    mysql> select * from user_role;
    +--------------+------------+
    | username     | role_name  |
    +--------------+------------+
    | apple        | admin      | 
    | orange       | groupAdmin | 
    | pineapple    | user       | 
    | peach        | user       | 
    +...........................+
     




Now, we know that the user_role does not have entity class (model). When I
write in my DAOHibernate, I want to get access to this user_role table in
order to get a list of username having role as 'user'. How can I write it in
my code....

    code:


    public class UserDaoHibernate extends GenericDaoHibernate<User, Long>
implements UserDao {
     
        public UserDaoHibernate() {
            super(User.class);
        }
     
    //   public List<User> listUsers() {
    //
    //         Query q = getSession().createQuery(" from User as u, Role as
r, //                    user_role as ur "    
    //                     + " where ur.username=u.username "
    //                     + " and ur.role_name = 'user'";
    //
    //
    // here I can not use 'user_role' as it will show errors.
    //
    //                return q;
    //    }
     
    }



I hope I made my doubt clear to you all. So Please help . Thank you.

GreenHorn


-- 
View this message in context: 
http://www.nabble.com/Need-help-for-Many-to-Many-association-tp17184793s2369p17184793.html
Sent from the AppFuse - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to