Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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);
}
}
}
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!