Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

HQL  RSS feed

 
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.
 
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);
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!