-->
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.  [ 2 posts ] 
Author Message
 Post subject: Need help with query with inheritance/association
PostPosted: Wed Sep 19, 2007 5:15 pm 
Newbie

Joined: Thu Sep 13, 2007 10:23 am
Posts: 2
First, my classes:

Permissions, like "Delete Users", "Modify Files"

Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
class Permission {
   @Id
   @Column(name = "permission_id")
   private Integer id;
   
   private String key;
   
}


An interface permissions, like "User Adminstration"

Code:
@Entity
@PrimaryKeyJoinColumn(name="permission_id")
class InterfacePermission extends Permission {
   private Integer type;
}


A role, like "Administrator", "Normal User"

Code:
@Entity
class Role {
   @Id
   @Column(name = "role_id")
   private Integer id;
   
   @ManyToMany(fetch = FetchType.LAZY,cascade = { CascadeType.PERSIST, CascadeType.MERGE })
   @JoinTable(name = "role_permissions",
         joinColumns = @JoinColumn(name ="role_id"),
         inverseJoinColumns = @JoinColumn(name = "permission_id"))
   private List<Permission> permissions;
   
}


And finally an user, like you and me...

Code:
@Entity
class User {
   @Id
   @Column(name = "user_id")
   private Integer id;
   
   @ManyToMany(fetch = FetchType.LAZY,cascade = { CascadeType.PERSIST, CascadeType.MERGE })
   @JoinTable(name = "user_role",
         joinColumns = @JoinColumn(name ="user_id"),
         inverseJoinColumns = @JoinColumn(name = "role_id"))
   private List<Role> roles;

   @ManyToMany(fetch = FetchType.LAZY,cascade = { CascadeType.PERSIST, CascadeType.MERGE })
   @JoinTable(name = "user_permissions",
         joinColumns = @JoinColumn(name ="user_id"),
         inverseJoinColumns = @JoinColumn(name = "permission_id"))
   private List<Permission> permissions;
}


What I want in criteria is to retrieve all the interface permissions of an user.. so that would be the union of his particular permissions and permissions from his roles.

Oh, and I want my criteria to retrieve an InterfacePermission.class. I tried many things, but couldn't find for example, if a permission is in user roles, and how I can join the role with the permssions in that context.

Any help would be appreciated.
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 11:25 am 
Newbie

Joined: Thu Sep 13, 2007 10:23 am
Posts: 2
in SQL, I could use UNION:

Code:
select
   ip.*
from
   interfacepermission ip,
   permission p
where
   p.permission_id=ip.permission_id and
   p.permission_id in (
      select
         rp.permission_id
      from
         role_permissions rp,
         user_role ur
      where
         ur.user_id = 1 and
         ur.role_id = rp.role_id
      union
      select
         up.permission_id
      from
         user_permissions up
      where
         up.user_id = 1
   )
order by p.key


or without UNION:

Code:
select
   ip.*
from
   interfacepermission ip,
   permission p
where
   p.permission_id=ip.permission_id and
   p.permission_id in (
      select
         rp.permission_id
      from
         role_permissions rp,
         user_role ur
      where
         ur.user_id = 1 and
         ur.role_id = rp.role_id
   ) or p.permission_id in (
      select
         up.permission_id
      from
         user_permissions up
      where
         up.user_id = 1
   )
order by p.key


But how I can do that in HQL, or how can I return InterfacePermissions entity with nativeQuery ???

The question remains...[/code]


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