Quote:
How can I load the Pais when it is required?
That really depends on how you are building your application. Basically, you have to understand what is going on in the background with regard to lazy initialization. Lazy collections can be instantiated on the fly if you still have the original session open, but that isn't always the best method. In most instances, the preferred method is to build the entire object graph necessary before it is forwarded to the View ( as in the MVC pattern).
If you follow the recommendations of the Hibernate team, all associations should be mapped lazy and you should build specific HQL queries to return specific instances of object graphs necessary for a particular purpose. In your case, you might write an HQL like so:
Code:
mySession.Find("from IAMCReportes.Common.Provincia Provincia join Provincia.Pais Pais");
An alternative would be to use the session.Lock() method and then initialze the lazily loaded attribute (property).
In my opinion, you shouldn't be lazily loading the Pais. Paises don't change much (even with world politics being what they are). I'd feel pretty safe allowing Pais to be eagerly fetched on the m-1 association and instead enable a "read-only" cache on the Pais object. It isn't going to change much and if it does, it will almost certinaly require a restart of the app. This way, NH will load the Pais from the cache and not the database. You could do this with a simple change to the mapping file:
Code:
<many-to-one name="Pais" class="Pais" outer-join="true">
<column name="IdPais" sql-type="int" not-null="true"/>
</many-to-one>
and the addition of the cache notation to the Pais mapping:
Code:
<class name="Pais" table="Paises" lazy="true" >
<cache usage="read-only" />
<id name="Id" type="Int32" unsaved-value="0">
<column name="IdPais" sql-type="int" not-null="true" unique="true" />
<generator class="native" />
</id>
...