Set lazy as true at runtime HQL
In our application, we have various objects set to lazy false based on the needs of the application. However, in one use case, we want to ignore all lazy settings in the HBM files and get ONLY the target.
So the question is, is there a way to tell HQL to fetch ONLY the target regardless of the HBM settings?
~ Sri
Sorry, not sure if you understood your question.
If you need to implement it for a specific class, you can simply use SetFetchMode.
var query = session.CreateCriteria(typeof(MyClass));
query.SetFetchMode("PropertyA", FetchMode.Select);
query.SetFetchMode("PropertyB", FetchMode.Select);
Note. For all-to-one links, the entity class itself must be mapped to lazy = true. If not, NHibernate doesn't even create a proxy class for it.
This is the answer if you want to be lazy loading a type in a generic, type-independent way:
You can find them with metadata and add sampling modes to criteria
I haven't tried it but would start with the following code:
var meta = sessionfactory.GetClassMetaData(typeof(MyClass));
var query = session.CreateCriteria(typeof(MyClass));
for(int index = 0; index < meta.PropertyType.Length; index++)
{
if (meta.PropertyType[index] == NHibernateUtil.Entity)
{
query.SetFetchMode(meta.PropertyNames[index], FetchMode.Select);
}
}
This does not include collections. They were probably found with factory.GetCollectionMetadata(roleName)
, but you need to find out roleName
.
a source to share