Содержание
- 2. Методы загрузки данных SQL query HQL query Criteria query QueryOver LINQ to NHibernate
- 3. SQL
- 4. Scalar queries ISession session = DbSessionFactory.Instance.OpenSession(); ISQLQuery query = session.CreateSQLQuery("SELECT * FROM CATS") .AddScalar("ID", NHibernateUtil.Int64) .AddScalar("NAME",
- 5. Entity queries ISQLQuery query = session.CreateSQLQuery("SELECT * FROM CATS") .AddEntity(typeof(Cat)); ISQLQuery query = session.CreateSQLQuery(@"SELECT ID, NAME,
- 6. Returning non-managed entities session.CreateSQLQuery("SELECT NAME, BIRTHDATE FROM CATS") .SetResultTransformer(Transformers.AliasToBean(typeof (CatDTO)));
- 7. Named SQL queries SELECT person.NAME AS {person.Name}, person.AGE AS {person.Age}, person.SEX AS {person.Sex} FROM PERSON person
- 8. Named SQL queries SELECT p.NAME AS name, p.AGE AS age, FROM PERSON p WHERE p.NAME LIKE
- 9. Named SQL queries SELECT person.NAME AS myName, person.AGE AS myAge, person.SEX AS mySex, FROM PERSON person
- 10. Using stored procedures for querying CREATE PROCEDURE selectAllEmployments AS SELECT EMPLOYEE, EMPLOYER, STARTDATE, ENDDATE, REGIONCODE, EMPID,
- 11. Using stored procedures for querying exec selectAllEmployments
- 12. Custom SQL for create/update/delete INSERT INTO PERSON (NAME, ID) VALUES ( UPPER(?), ? ) UPDATE PERSON
- 13. Stored procedures for create/update/delete exec createPerson ?, ? exec deletePerson ? exec updatePerson ?, ?
- 14. Custom SQL for loading SELECT NAME AS {pers.Name}, ID AS {pers.Id} FROM PERSON WHERE ID=? FOR
- 15. HQL
- 16. Scalar and entity queries string hql = "from Product p"; var products = session.CreateQuery(hql).List(); var products
- 17. Filtering, sorting and paging string hql = @"from Product p where p.Discontinued and p.Category = :category
- 18. Unique result IQuery query = session.CreateQuery("select count(*) from Product"); int count = Convert.ToInt32(query.UniqueResult()); int count =
- 19. Result transformers var productsLookup = session .CreateQuery("select Id as Id, Name as Name from Product") .SetResultTransformer(Transformers.AliasToBean
- 20. Grouping var productsGrouped = session .CreateQuery(@"select p.Category as Category, count(*) as Count, avg(p.UnitPrice) as AveragePrice from
- 21. Grouping with transformers var productsGrouped = session .CreateQuery(@"select p.Category as Category, count(*) as Count, avg(p.UnitPrice) as
- 22. Join var products = session .CreateQuery(@"select p from Product p left join p.Category as c left
- 23. Multi query public void GetPageOfProducts(int pageNumber, int pageSize) { ISession session = DbSessionFactory.Instance.OpenSession(); int skip =
- 24. Named queries select count(p.Id) from Product p ]]> from Product p order by p.UnitPrice asc ]]>
- 25. Named queries & futures public void GetPageOfProducts(int pageNumber, int pageSize) { ISession session = DbSessionFactory.Instance.OpenSession(); var
- 26. Detached query string hql = @"from Product p where p.Discontinued"; IDetachedQuery detachedQuery = new DetachedQuery(hql); IQuery
- 27. Bulk data changes var updateHql = "update Product p set p.UnitPrice = 1.1 * p.UnitPrice"; session.CreateQuery(updateHql).ExecuteUpdate();
- 28. Criteria
- 29. Restrictions List products = session.CreateCriteria () .Add(Restrictions.Eq("Name", productName)) .AddOrder(Order.Asc("UnitPrice")) .List ();
- 30. Restrictions Eq, EqProperty Ge, Gt, GeProperty, GtProperty Le, Lt, LeProperty, LtProperty Like In Between Not IsNull
- 31. Restrictions List products = session.CreateCriteria () .Add(Restrictions.And( Restrictions.Ge("UnitPrice", minPrice), Restrictions.Le("UnitPrice", maxPrice) )) .AddOrder(Order.Asc("UnitPrice")) .List ();
- 32. Join IList categories = session.CreateCriteria () .CreateCriteria("Products", JoinType.InnerJoin) .Add(Restrictions.Eq("Discount", 0)) .List ();
- 33. Paging List products = session.CreateCriteria () .Add(Restrictions.Eq("Name", productName)) .SetFirstResult(10) .SetMaxResults(10) .List ();
- 34. Projections IList products = session.CreateCriteria () .Add(Restrictions.Eq("Name", productName)) .SetProjection(Projections.ProjectionList() .Add(Projections.Property("Id")) .Add(Projections.Property("Name"))) .SetResultTransformer(Transformers.AliasToBean(typeof(NameID))) .List();
- 35. Aggregate functions var productCount = session.CreateCriteria () .Add(Restrictions.Eq("Name", productName)) .SetProjection(Projections.RowCount()) .UniqueResult();
- 36. Multi criteria public void GetPageOfProducts(int pageNumber, int pageSize) { ISession session = DbSessionFactory.Instance.OpenSession(); int skip =
- 37. Detached criteria DetachedCriteria detachedCriteria = DetachedCriteria.For () .Add(Restrictions.Like("Name", productName)); IList results = detachedCriteria.GetExecutableCriteria(session).List();
- 38. QueryOver
- 39. QueryOver IList results = session.QueryOver () .Where(x => x.ProductType == ProductTypes.ProductTypeA) .OrderBy(x => x.Name) .Desc .List();
- 40. QueryOver IList results = session.QueryOver () .WhereRestrictionOn(x => x.UnitPrice) .IsBetween(20).And(50) .OrderBy(x => x.Name) .Desc .List();
- 41. QueryOver IList results = session.QueryOver () .Select(m => m.Name, m => m.UnitPrice) .List () .Select(props =>
- 42. Projections var result = session.QueryOver () .Select(Projections.Avg (m => m.UnitPrice)) .SingleOrDefault ();
- 43. Join, paging IList categories = session.QueryOver () .Inner.JoinQueryOver(x => x.Products) .Skip(20) .Take(10) .List ();
- 44. Session methods
- 45. Session actions using (ISession session = DbSessionFactory.Instance.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { // create,
- 46. Session methods Insert Save Update SaveOrUpdate Delete Get Load Merge Flush Refresh Evict Clear Close
- 48. Скачать презентацию