Getting Started with NHibernate (v)

Source: Internet
Author: User
Tags generator rowcount
Criteriaquery and hqlquery knowledge points project structure as shown:

Model class Library is as follows Customer.cs class and Customer.hbm.xml entity mapping files

[Serializable]
    public class Customer
    {public
        int Id {get; set;}
        public string FirstName {get; set;}
        public string LastName {get; set;}

        Public address address {get; set;}

        Public byte[] Dbversion {get; set;}
        public int Version {get; set;}
        Public DateTime Timestamp {get; set;}

        Public ilist<order> Orders {get; set;}

        Public Customer ()
        {
            Orders = new list<order> ();
        }
    }
<?xml version= "1.0" encoding= "Utf-8"?> 
Order.cs class and Order.hbm.xml entity mapping files
[Serializable]
    public class Order
    {public
        int Id {get; set;}
        Public decimal Value {get; set;}
        Public ilist<category> Categories {get; set;}

        Public customer customer {get; set;}

        Public order ()
        {
            Categories = new list<category> ();
        }
    }
<?xml version= "1.0" encoding= "Utf-8"?> 
hibernate.cfg.xml Global Mapping File
<?xml version= "1.0" encoding= "Utf-8"?>  
 CriteriaQuery.cs Query Class   
 public class Criteriaquery {isessionfactory sessionfactory = new Configuration (). Configure ().


        Buildsessionfactory ();
                public void Simplequeries () {using (ISession session = Sessionfactory.opensession ()) { var result = (int) session. Createcriteria (typeof (Customer)). Setprojection (Projections.rowcount ()).

                Uniqueresult (); Session. Createcriteria (typeof (Customer)). Createcriteria ("Orders", "O"). ADD (restrictions.gt ("O.value", 90m)).

                List<customer> (); Session. Createcriteria<customer> (). Createcriteria ("Orders", "O"). Setprojection (Projections.rowcount ()). Setprojection (Projections.groupproperty ("LastName")). Setprojection (Projections.max ("O.value")).

                List (); foreach (Customer customer in session.) CreateCriteria<customer> (). List<customer> ()) {int ordercound = Customer.
                Orders.count;
            } Console.WriteLine ("Simplequeries_customercount:" + result); }//NHibernate3 new query API Queryover.
        This section will introduce you to the relevant content of the query API Queryover.
        Queryover is built on top of NHibernate's original Icriteria API, which supports lambda expressions and extension methods.
                public void Queryover () {using (ISession session = Sessionfactory.opensession ()) { int result = Session. Queryover<customer> ().

                RowCount (); Ilist<customer> customers = Session. Queryover<customer> (). Joinqueryover<order> (Cust => Cust. Orders). Where (o => o.value > 90).

                List<customer> (); ilist<object> results = session. Queryover<customer> ().
                    SelectList (List =>        List. Selectgroup (c => c.lastname)). Joinqueryover<order> (Cust => Cust. Orders).
                List<object> ();
            Console.WriteLine ("Queryover_customercount:" +result); }
        }

    }
HQLQuery.cs Query Class
  public class Hqlquery
    {
        isessionfactory sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();

        public void Simplequeries ()
        {
            using (isession sessions = Sessionfactory.opensession ())
            {
                Long result = ( Long) session. CreateQuery ("SELECT count (*) from Customer"). Uniqueresult ();
                Ilist<customer> customers = Session
                    . CreateQuery ("Select Cust from Customer as Cust inner join Cust.") Orders as order where order. Value > 90 "). List
                        <Customer> ();

                IList results = session. CreateQuery (
                    "Select Cust.") LastName, Max (O.value) from the Customer as Cust inner join Cust. Orders as O GROUP by Cust. LastName "). List ();

                Console.WriteLine ("Hqlquery_simplequeries:" + result);}}
    
Main program
Class program
    {
        static void Main (string[] args)
        {
            //criteriaquery query = new Criteriaquery ();
            Query. Simplequeries ();
            Query. Queryover ();
            ---------------------------------------------------
            hqlquery hq=new hqlquery ();
            Hq. Simplequeries ();
            Console.readkey ();
        }
    
Run Results

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.