icriteria

NHibernate Criteria Query - Select Distinct with Joined Entity

纵然是瞬间 提交于 2019-12-02 03:06:32
问题 I have a Person entity. Every person has a country, I want to select all the distinct countries that have people in them. This Criteria Query returns all the distinct CountryID's criteria.SetProjection(Projections.Distinct(Projections.Property("Country"))); How do I alter it to join and fetch the Country entity, not just the ID? 回答1: Any easy way would be to use a subquery. That is, you could select the whole country on the outer query where the country ID matches the inner query. Subqueries

NHibernate Criteria Query - Select Distinct with Joined Entity

故事扮演 提交于 2019-12-02 02:34:54
I have a Person entity. Every person has a country, I want to select all the distinct countries that have people in them. This Criteria Query returns all the distinct CountryID's criteria.SetProjection(Projections.Distinct(Projections.Property("Country"))); How do I alter it to join and fetch the Country entity, not just the ID? Any easy way would be to use a subquery. That is, you could select the whole country on the outer query where the country ID matches the inner query. Subqueries.PropertyIn( "Country", innerDetachedCriteriaWhichFindsCountriesWithPeopleAndProjectsCountryId) 来源: https:/

Can first level cache be used with ICriteria or other APIs?

◇◆丶佛笑我妖孽 提交于 2019-12-01 18:31:19
In NHibernate you can easily benefit from first level cache when using Load or Get methods. But what about ICriteria , HQL , Linq-to-NHibernate and QueryOver ? Do they use first level cache too? They use it for returning entities, but the queries go straight to the db unless you use the second level cache. Consider this: var fooUsingGet = session.Get<Foo>(fooId); var fooQueryById = session.Query<Foo>().Single(f => f.Id == fooId); Two queries are executed (one for the Get, one for the Query), but both variables contain the same object reference. Now, if you enable the 2nd level cache, query

How to set more than 2 Expression in Expression.Or

时光怂恿深爱的人放手 提交于 2019-12-01 16:26:15
I want to create a query which has more than 3-4 Expression.Or ? But Expression.Or just let me to add two Expressions inside it. if (!string.IsNullOrEmpty(keyword)) query .Add(Expression.Or( Expression.Like("Name", keyword, MatchMode.Anywhere), Expression.Like("LastName", keyword, MatchMode.Anywhere))) .Add(Expression.Or( Expression.Like("Email1", keyword, MatchMode.Anywhere), Expression.Like("Email2", keyword, MatchMode.Anywhere))); The code above generates "Name like %this% or LastName like %this% AND Email1 like %this% and Email2 like %this. Thanks in advance. Use Disjunction instead of Or.

Nhibernate Criteria: 'select max(id)…'

家住魔仙堡 提交于 2019-11-30 13:14:51
Can I use a Criteria to execute a t-sql command to select the max value for a column in a table? 'select @cus_id = max(id) + 1 from customers' Ta Ollie Use Projection : session.CreateCriteria(typeof(Customer)) .SetProjection( Projections.Max("Id") ) . UniqueResult(); Max(id) + 1 is a very bad way to generate ids. If that's your goal, find another way to generate ids. Edit: in answer to LnDCobra: it's bad because it's hard to make sure that the max(id) you got is still the max(id) when you do the insert. If another process inserts a row, your insert will have the same id, and your insert will

Order by collection count using ICriteria & NHibernate

余生颓废 提交于 2019-11-30 09:51:48
问题 Using the standard NHibernate example of Cats and Kittens, how would I use ICriteria to sort Cats based on Kitten count? For example, I want to do something like: ICriteria crit = Session.CreateCriteria(typeof(Cat)); return crit.Order(Order.Asc("**Kittens.Count**")); Anyone know how to achieve this? 回答1: In HQL you can do it like this: select cat from Eg.Cat cat join cat.Kittens kitten group by cat order by count(kitten) asc 回答2: See http://forum.hibernate.org/viewtopic.php?p=2401219 It's

Lazy loading not working for many-to-one relationship when mapping to a non-key field using property-ref

陌路散爱 提交于 2019-11-29 05:39:17
I have a legacy database that I am mapping using NHibernate. The objects of concern are an Account and a list of Notification objects. The objects look like: public class Notification { public virtual int Id { get; set; } public virtual DateTime BatchDate { get; set; } /* other properties */ public virtual Account Account { get; set; } } public class Account { public virtual int Id { get; set; } public virtual string AccountNumber { get; set; } /* other properties */ } The mapping files look like: <class name="Account" table="Account" dynamic-update="true"> <id name="Id" column="AccountID">

NHibernate Criteria - How to filter on combination of properties

余生颓废 提交于 2019-11-29 02:39:31
I needed to filter a list of results using the combination of two properties. A plain SQL statement would look like this: SELECT TOP 10 * FROM Person WHERE FirstName + ' ' + LastName LIKE '%' + @Term + '%' The ICriteria in NHibernate that I ended up using was: ICriteria criteria = Session.CreateCriteria(typeof(Person)); criteria.Add(Expression.Sql( "FirstName + ' ' + LastName LIKE ?", "%" + term + "%", NHibernateUtil.String)); criteria.SetMaxResults(10); It works perfectly, but I'm not sure if it is the ideal solution since I'm still learning about NHibernate's Criteria API. What are the

Joining two unrelated view-tables with nhibernate and ICriteria

蓝咒 提交于 2019-11-28 12:31:44
问题 I have two entities based on two views. The mappings looks like this: Entiy A: <class name="SearchView" table="SearchView" dynamic-update="true" mutable="false" schema-action="none"> <id name="Id" type="Guid" column="Id" /> <property name="Id" column="Id" type="Guid" /> <property name="Expires" column="Expires" type="DateTime" /> <property name="VerificationNumber" column="VerificationNumber" type="Int32" /> <property name="InvoiceNo" column="InvoiceNo" type="Int32" length="50" /> <property

Lazy loading not working for many-to-one relationship when mapping to a non-key field using property-ref

左心房为你撑大大i 提交于 2019-11-27 23:15:13
问题 I have a legacy database that I am mapping using NHibernate. The objects of concern are an Account and a list of Notification objects. The objects look like: public class Notification { public virtual int Id { get; set; } public virtual DateTime BatchDate { get; set; } /* other properties */ public virtual Account Account { get; set; } } public class Account { public virtual int Id { get; set; } public virtual string AccountNumber { get; set; } /* other properties */ } The mapping files look