Examples of Object Query ing in nhib.pdf Stored Procedure

Source: Internet
Author: User

There are not many articles on the NHibername stored procedure, and there are basically just a few examples on the Internet, especially for the stored procedure that returns the record set. According to my research summary, this article is intended for MySql databases, and some details of other databases are slightly different.

1. Create a stored procedure:

DELIMITER $$DROP PROCEDURE IF EXISTS `test_get_prices` $$CREATE DEFINER=`admin`@`%` PROCEDURE `test_get_prices`()BEGIN  select a.id as id,a.xibie as xb,b.checi as cc,a.OStation as os,a.DStation as ds from noshow_data as a, phone_book_data as b where a.checi=b.checi and a.date=b.go_date;END $$DELIMITER ;

This is a stored procedure for returning record sets. It may span several tables and only take some fields of some tables. It may also be a temporary field consisting of sum and other results calculated based on other values.

It is worth noting that if you want to use NHibernate to map the result set of the stored procedure, you must ensure that the returned result set has a primary key, this is because the mapping File of NHibernate must have a primary key definition item. I have not tried the method of generating a primary key for the result set without a primary key or the mapping File ).

2. Compile the Model class:

public class ProcRequestPrice    {        public virtual int Id { get; set; }        public virtual string Checi { get; set; }        public virtual string Xibie { get; set; }        public virtual string OStation { get; set; }        public virtual string DStation { get; set; }    }

The fields of the Model class are determined based on the result set returned by the stored procedure.

3. Compile the NHibernate ing file:

<?xml version="1.0" encoding="utf-8"?>

Mutable = "false" indicates that the read-only object of this object is unchangeable. GetRequestPrices is the identifier used for calling in DAL. The ing table name is not specified here, because the ing is not an entity table and is the result set returned by the stored procedure.

MySql is call test_get_prices () and SqlServe is exec test_get_prices (). For details about parameters, see:

Http://www.cnblogs.com/lyj/archive/2008/11/03/1325291.html

Http://www.cnblogs.com/lyj/archive/2008/11/06/1328240.html

Http://www.cnblogs.com/lyj/archive/2008/11/07/1328782.html

Remember to set the Mapping File as an embedded resource.

4. Compilation of call and test programs

This test program is written based on NUnit:

[TestFixture]public class CustomerTest{    private NHibernateHelper nhibernateHelper = new NHibernateHelper();    [Test]    public void ProcedureTest()    {        try        {            IQuery query = nhibernateHelper.GetSession().GetNamedQuery("GetRequestPrices");            List<ProcRequestPrice> rs = query.List<ProcRequestPrice>() as List<ProcRequestPrice>;            Console.Out.WriteLine("count:" + rs.Count);            foreach (ProcRequestPrice rp in rs)            {                Console.Out.WriteLine("value:" + rp.Id + "," + rp.Checi + "," + rp.Xibie + "," + rp.OStation + "," + rp.DStation);            }                                                             }        catch (Exception e)        {            Console.Out.WriteLine(e.StackTrace.ToString());        }    }}


The test is successful.

In addition, there is currently an open-source NHibernate O/R Mapping generation tool: NHibernateMappingGenerator, but it is still in the development stage. There are many bugs and only Model and Mapping files can be generated, I have fixed several bugs in the latest version, added the generation of DAL and IDAL, and the generation tool can merge the customized methods of DAL. Wait for the whole task to be sent.


This article is from the "little he Beibei's technical space" blog, please be sure to keep this http://babyhe.blog.51cto.com/1104064/1263737

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.