[Nhib.pdf] use of stored procedures (III)
The previous article introduced how to use the stored procedure to add, delete, and modify in nhib.pdf. Of course, the query is also possible, and any stored procedure can be executed in nhib.pdf. This article describes how to use the stored procedure of the query. [Nhibernate] architecture [NHibernate] ISessionFactory configuration [NHibernate] Persistent Classes) [nhibses] O/R Mapping basics [nhibses] collection Classes) ing [nhib] ing [nhib] Parent/Child [NHibernate] cache (NHibernate. caches) [nhib.pdf] nhib.pdf. tool. hbm2net [nhibles] Nullables [nhibles] How does nhib map the [NHibernate] basic configuration and test [NHibernate] HQL Query [Nhibernate] condition Query Criteria Query [NHib Pipeline] add, delete, modify, and [NHibernate] transaction [NHibernate] concurrency control [NHibernate] dependent object of the component [NHibernate] one-to-many relationship (cascade deletion, cascade addition) [nhib] one-to-many relationship (join query) [NHibernate] multiple-to-many relationship (join query) [nhibib] delayed loading [NHibernate] Loading now [NHibernate] view processing [NHibernate] N + 1 Select query problem analysis [NHibernate] use of stored procedures (I) [nhib.pdf] use of stored procedures (2) query using nodes <SQL-query> query by user ID add Stored Procedure copy Code create proc [dbo]. [ps_Search] @ CustomerID uniqueidentifierasbegin select * from TB_Customer w Here CustomerID = @ CustomerID end the copy Code uses <SQL-query> In the ing file and defines <SQL-query> query name 1 <! -- Must be at the same level as the class node --> 2 <SQL-query name = "ps_Search"> 3 <return class = "Wolfy. shop. domain. entities. customer, Wolfy. shop. domain "/> 4 exec ps_Search: CustomerID5 </SQL-query> in the data access layer, the GetNamedQuery method provided by the ISession interface is used to call a stored procedure with a name, and pass an integer parameter. The Code is as follows: Copy code 1 // <summary> 2 // use the stored procedure, query 3 // </summary> 4 // <returns> </returns> 5 public IList <Customer> SearchCustomerByIDUsingProc (Guid customerID) 6 {7 ISession session = NHibernateHelper. getSession (); 8 // The parameter is the name 9 10 return session specified for the <SQL-query> node in the ing file. getNamedQuery ("ps_Search") 11. setGuid ("CustomerID", customerID) 12. list <Customer> (); 13} copy the SQL statement 1 exec sp_executesql n' exec p generated by the code result S_Search @ p0 ', n' @ p0 uniqueidentifier', @ p0 = 'ddf63750-3307-461b-b96a-7ff356540cb8' what if I want to return some attributes of an object? Modify the stored procedure and select only the required field to copy code 1 ALTER proc [dbo]. [ps_Search] 2 @ CustomerID uniqueidentifier3 as4 begin5 select CustomerName from TB_Customer6 where CustomerID = @ CustomerID7 end copy the code to modify the ing file <! -- Must be at the same level as the class node --> <SQL-query name = "ps_Search"> <! -- <Return class = "Wolfy. shop. domain. entities. customer, Wolfy. shop. domain "/> --> <return-scalar column =" CustomerName "type =" String "/> exec ps_Search: customerID </SQL-query> modification method copy code 1 // <summary> 2 // use the stored procedure, query 3 /// </summary> 4 /// <returns> </returns> 5 public string SearchCustomerNameUsingProc (Guid customerID) 6 {7 ISession session = NHibernateHelper. getSession (); 8 // The parameter is specified by the <SQL-query> node in the ing file. Name 9 return session. getNamedQuery ("ps_Search") 10. setGuid ("CustomerID", customerID ). uniqueResult (). toString (); 11} because the stored procedure is executed, the SQL statement is the same as the preceding one (modified based on the original stored procedure ). If the most common bug encounters such an exception, you can ignore it. I haven't found a solution for a long time, and then I directly returned the result in. List <Customer>. Although the results are always unsatisfactory (perfectionist ). This article summarizes how to use the Stored Procedure query method in nhib.pdf and what you need to pay attention to. For example, the <SQL-query> node in the ing file must be at the same level as the <class> node. Now, adding, deleting, modifying, and querying stored procedures in nhib.pdf is over. To reduce the number of SQL statements, it is rare to use the orm in actual projects. Even if it is used, there is no way to directly add <SQL-**> to the ing file. As you know, it may be difficult for others to know about the maintenance. However, it is okay to know what is going on when you know about this tall writing method.