For example, say I have two tables, Product
and Order
. Product has ID
, Name
, Description
, Cost
, and other detailed columns. Order has ID
and ProductID
columns (assume an order can only contain one product).
When displaying a list of orders in the system, I would like to also display the associated product name without all of the other data (ie, eagerly load an order and its associated product name, and lazily load all of the other product properties):
SELECT o.ID, o.ProductID, p.Name FROM Order o JOIN Product p ON o.ProductID=p.ID
If I do this with NHibernate, I have two choices: eager loading or lazy loading.
With eager loading, I get something like:
SELECT o.ID, o.ProductID, p.ID, p.Name, p.Description, p.Cost, p.... FROM Order JOIN Product p ON o.ProductID=p.ID
With lazy loading, I get something like:
SELECT o.ID, o.Product ID from Order
....
SELECT p.Name, p.Description, p.Cost, p.... FROM Product p WHERE p.ID=?
Update
Here is a more concrete example of what I am trying to achieve. I am working with an existing DAL and trying to integrate NHibernate. One of the functions of the current DAL is that it allows retrieval of some basic foreign key information as part of the parent record. Let's say there is a User
table and a Region
table. Each user has a foreign key to the region in which they reside. When displaying user information in a GUI, the region name should be displayed with the user, but other details about the region are not necessary.
In the current DAL, the User
domain object has a member of type ForeignKeyReference<Region>
.
public class ForeignKeyReference<T>
{
public virtual int ForeignKeyID { get; set; }
public virtual string ForeignNaturalKey { get; set; }
public virtual T Reference { get; set; }
}
When the User
is retrieved from the database, the primary and natural keys for the Region
are also retrieved, and the Reference
is set to a proxy object. I would like to simplify this with NHibernate, but still maintain this functionality. For example, I would like to remove the ForeignKeyReference<Region>
member and just have a Region
member which is an NHibernate proxy. On this proxy, I would like to be able to retrieve the ID and the Name without having to hit the database again.
You can mark a specific column as lazy. Ayende wrote about this new feature last year.
I would suggest you to read the Hibernate documentation about this feature, though:
Hibernate3 supports the lazy fetching of individual properties. This optimization technique is also known as fetch groups. Please note that this is mostly a marketing feature; optimizing row reads is much more important than optimization of column reads. However, only loading some properties of a class could be useful in extreme cases. For example, when legacy tables have hundreds of columns and the data model cannot be improved.
The other alternative is to creare your own queries:
Session.CreateQuery
Have a look at this answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.