• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

HQL

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic