Hello,
I am new to NHibernate and am wondering if there is an overwhelming
query direction to use CreateCriteria or CreateQuery. On the personal
side, I like writing the SQL so I think I would like the CreateQuery
better, however, I just ran into a problem when I tried to use it. I
had a query that looked like:
var rows = session.CreateQuery("from RptCountry").List<RptCountry>();
The C# class looks like:
public class RptCountry {
public RptCountry() { }
public virtual long CountryId { get; set; }
public virtual RptOfficer RptOfficer { get; set; }
public virtual string CountryName { get; set; }
public virtual string CountryCode { get; set; }
}
and the mapping file looks like:
<hibernate-mapping assembly="FirstSample"
namespace="FirstSample.Domain" xmlns="urn:nhibernate-mapping-2.2">
<class name="RptCountry" table="RPT_COUNTRY" lazy="true" >
<id name="CountryId" type="Int64" column="COUNTRY_ID">
<generator class="sequence">
<param name="sequence">RPT_COUNTRY_SEQ</param>
</generator>
</id>
<many-to-one name="RptOfficer" column="OFFICER_ID" fetch="join"/>
<property name="CountryName" column="COUNTRY_NAME" />
<property name="CountryCode" column="COUNTRY_CODE" />
</class>
</hibernate-mapping>
The problem is that it looks like the CreateQuery ignores the
"fetch=join" attribute of the <many-to-one> element which causes a
runtime error if I try to access any property of the RptOfficer object
other than the OFFICER_ID.
If I use the CreateCriteria code instead:
var rows =
session.CreateCriteria(typeof(RptCountry)).List<RptCountry>();
All is well. I thought I read something that CreateCriteria does
ignore some mapping items but I did not get a clear picture of which
ones.
Are there any thoughts on which is better to use, such as "it is
better to use CreateCriteria because you don't have mapping file
issues" or "it is better to use CreateQuery because it easier to
understand and mapping issues can be fixed by..."
Any thoughts would be greatly appreciated.
Thanks - Peter
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.