Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HQL

 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using user table to fetch the user details
and role table to fetch the roles and description
And we have one more table of user_role_link table in which user_id and role_id is associated in it.
user table doesn't have the role_id and role table doesn't have the user_id,
based on this i want to write a HQL to fetch userlist based of role_id.
 
k.siva prasadreddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I hope the following example can help you.


public class Role
{

private Integer roleId;
private String name;
private Set<User> users;
//setters getters
}

public class User
{
private Integer userId;
private String userName;
//setters getters
}

<class name="User" table="Users">
<id name="userId" column="user_id">
<generator class="native"/>
</id>
<property name="userName" column="username"/>

</class>

<class name="Role" table="ROLES">
<id column="role_id" name="roleId">
<generator class="native"></generator>
</id>
<property name="name" column="role_name"></property>
<set name="users" table="users_roles">
<key column="role_id"></key>
<many-to-many column="user_id" class="User"/>
</set>
</class>


Query query = session.createQuery("from Role where roleId = ?");
query.setInteger(0, 1);
List<Role> roles = query.list();
if(roles!=null){
for (Role role : roles) {
Set<User> users = role.getUsers();
for (User user2 : users) {
System.out.println(user2);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic