Given your request, you could simply not map from Department to Employees, nor have an Employees property on your department. This would mean you always have to make a database hit to find the employees of a database.
Aplogies if these code examples don't work out of the box, I'm not near a compiler at the moment
So, your department class might look like:
public class Department
{
public int Id { get; protected set; }
public string Name { get; set; }
/* Equality and GetHashCode here */
}
and your Employee would look like:
public class Employee
{
public int Id { get; protected set; }
public Name Name { get; set; }
public Department Department { get; set; }
/* Equality and GetHashCode here */
}
Any time you wanted to find Employees for a department, you've have to call:
/*...*/
session.CreateCriteria(typeof(Employee))
.Add(Restrictions.Eq("Department", department)
.List<Employee>();
Simply because your spec says "Departments have many Employees", doesn't mean you have to map it as a bi-directional association. If you can keep your associated uni-directional, you can really get your data-access to fly too.
Google "Domain Driven Design" Aggregate, or see Page 125 of Eric Evan's book on Domain Driven Design for more information
Do an "inner join fetch" on the Account.Order.Product entity. So instead of something like this (which is what you probably already have):
"from Account a inner join fetch a.Order where a.ID = ?"
Tell it to fetch the Order.Product as well:
"from Account a inner join fetch a.Order inner join fetch a.Order.Product where a.ID = ?"
Best Answer
Yes, but…
The level of flexibility you have here may depend on your version of NHibernate and how you are building your query. For example, a LINQ query (here is where versioning may make a difference) is not going to give you the same flexibility as an
ICriteria
or HQL query.With the criteria API, you can call
.SetFetchMode()
, passing in the property and the desired mode for that query.NHibernate also allows you to create projections, so you can instantiate objects of unmapped types or DTOs without mapping. Of course, a projection goes only one-way; if it isn't mapped, it can't be persisted.