NHibernate QueryOver with Many-to-Many


S

Stefan Niculescu

I'm in the process of learning QueryOver, but I can't figure out how to do a simple many to many query.

I have three tables, Users, Roles, and a junction UserRoles.

Users UserRoles Roles
======= ============= =========
UserId UserId RoleId
Username RoleId Role
Firstname Date
My tables are joined like this:

Users.UserId -> UserRoles.UserId
Roles.RoleId -> UserRoles.RoleId
I'm trying to select the role for a spefic username using QueryOver basically the alternative for:

SELECT
dbo.Roles.Role
FROM dbo.Roles
INNER JOIN dbo.UserRoles
ON dbo.Roles.RoleId = dbo.UserRoles.RoleId
INNER JOIN dbo.Users
ON dbo.UserRoles.UserId = dbo.Users.UserId
WHERE (Username = @Username)
Classes:

public class Roles
{
public virtual int RoleId { get; set; }
public virtual string Role { get; set; }
}
public class UserRoles
{
public virtual int UserId { get; set; }
public virtual int RoleId { get; set; }
public virtual DateTime Date { get; set; }
}
public class Users
{
public virtual int UserId { get; set; }
public virtual string Username { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual DateTime Date { get; set; }
}
Mappings:

<class name="Roles">
<id name="RoleId">
<generator class="native" />
</id>
<property name="Role" />
</class>

<class name="UserRoles">
<property name="UserId" />
<property name="RoleId" />
<property name="Date" />
</class>

<class name="Users">
<id name="UserId">
<generator class="native" />
</id>
<property name="Username" />
<property name="FirstName" />
<property name="LastName" />
<property name="Email" />
<property name="Date" />
</class>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NHibernate And Many-To-One Mapping 1
Many-To-Many Data 1
Traverse the Many-to-Many relationship? 1
Nhibernate 4
nHibernate 0
NHibernate 3
How many threads is too many? 6
Linq Many to Many relationship and XML 7

Top