- Posted by liammclennan on May 22, 2009
The only book on NHibernate, NHibernate In Action, conveniently fails to demonstrate how to perform a query that requires a many-to-many join. Suppose you are implementing a security system, where each user has access to a set of doors and each door is accessible by a set of user. There is a many-to-many relationship between User and Door. Here is how you could query for all the doors that a user has access to:
session.CreateQuery("from Door door left join fetch door.Users user where user.Username = :username")
.SetString("username", username);
The 'fetch' keyword causes an outer join between Door and User, transparently utilizing the connecting table.