3. ... and it is translated just ones instead at each execution. btw NH has
a cache of the last 256 queries translated to SQL.

On Wed, Sep 1, 2010 at 3:42 PM, Jason Meckley <[email protected]>wrote:

> CreateCriteria is use when you build up a query dynamically at run
> time. the best example of this is a search screen where the user can
> select from a variety of options to find the results they are looking
> for.
>
> CreateQuery is a good option when the structure of the query does not
> change, only the parameters. for example:
> from Customer c join fetch c.Orders o where c.id = :id and o.Date
> between :start and :end
> this would select a specific customer and all the orders within a data
> range
>
> you can also define the HQL queries in hbm resources like this
> <query name="CustomerWithOrdersWithinDateRange">
>  <param name="id" type="long" />
>  <param name="start" type="DateTime" />
>  <param name="end" type="DateTime" />
>  from Customer c join fetch c.Orders o where c.id = :id and o.Date
> between :start and :end
> </query>
>
> you can then call this query using
> var customer = session
>         .GetNamedQuery("CustomerWithOrdersWithinDateRange")
>         .SetParameter("id", id)
>         .SetParameter("start", DateTime.Today.AddDays(-7))
>         .SetParameter("end", DateTime.Today)
>         .SetResultTransformer(Transformers.DistinctRootEntity)
>         .UniqueResult<Customer>();
>
> the advantages of named queries are
> 1. the query details are not part of the code
> 2. queries can be compiled and validated when the session factory is
> built. If there is a problem with the query you will know immediately,
> rather than when the query executes. (although unit tests should cover
> them as well)
> 3. I believe there is also a preformance gain by using named queries
> because they are compiled when the factory is built, rather than when
> the query is executed.
> 4. reuse
>
>
> On Sep 1, 2:16 pm, PLen <[email protected]> wrote:
> > 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]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Fabio Maulo

-- 
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.

Reply via email to