NHibernate Criteria: howto exclude certain mapped properties/collections?

↘锁芯ラ 提交于 2019-12-24 08:20:04
问题 Here's my (simplified) model: Ticket -> Customer Callback (s) I have my Ticket mapped so that when it's loaded, the Callbacks are as well. base.HasMany<TechSupportCallback>(x => x.Callbacks) .KeyColumn(Fields.TRACKED_ITEM_ID) .Not.LazyLoad() .Inverse() .Cache.ReadWrite(); This is not lazy loading because otherwise I'll get 'no session to load entities' when the web service tries to serialize (and load) the proxy. (Using repositories to fetch data.) It's also bi-directional .. (in the

NHibernate - Paging with ICriteria and optional ICriteria calls

半城伤御伤魂 提交于 2019-12-23 02:17:10
问题 I want to do something like this... return GetSession() .ToPagedList<Employee>(page, pageSize, x=> x.SetFetchMode(DomainModelHelper.GetAssociationEntityNameAsPlural<Team>(), FetchMode.Eager)); But I don't know how to pass this Func<ICriteria,ICriteria> into the ISession or ICriteria . I have a standard paging extension method and this extension method shall have an overload where I can pass additional ICriteria methods, so that I can additionally set up the FetchMode or something else.

NHibernate Projections and “Having” clause

泄露秘密 提交于 2019-12-22 05:54:47
问题 I'm using NHibernate to query my database with the criteria API. My criteria is below: ICriteria c = Session.CreateCriteria(typeof(Transaction)); ProjectionList projections = Projections.ProjectionList(); projections.Add(Projections.Sum("Units"), "Units"); projections.Add(Projections.GroupProperty("Account"), "Account"); projections.Add(Projections.GroupProperty("Security"), "Security"); c.SetProjection(projections); This is working fine, but what I would like is a way to be able to limit the

Nhibernate ICriteria and Using Lambda Expressions in queries

自古美人都是妖i 提交于 2019-12-21 21:16:00
问题 hi i am new in NHibernate and i am a little confused. Suppose we have a product table. Let the product table have 2 columns price1 and price2. then i may query mapped product entities via HQL as follows: string queryString = @"from product p where p.price1 = p.price2 + 100 "; IList result = session.CreateQuery(queryString).List(); How can i achieve that via ICriteria API. i know it's absurd but i am trying sth like that: session.CreateCriteria(typeof(product)) .Add(Expression.Eq("price1",

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

[亡魂溺海] 提交于 2019-12-20 01:10:01
问题 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? 回答1: 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

How to set more than 2 Expression in Expression.Or

二次信任 提交于 2019-12-19 16:52:24
问题 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

Nhibernate Criteria: 'select max(id)…'

风流意气都作罢 提交于 2019-12-18 15:50:10
问题 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 回答1: Use Projection: session.CreateCriteria(typeof(Customer)) .SetProjection( Projections.Max("Id") ) . UniqueResult(); 回答2: 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)

How do I select a Random Row using NHibernate's ICriteria API?

纵然是瞬间 提交于 2019-12-17 16:11:31
问题 Can I select a random row using NHibernate's ICriteria API? 回答1: Just as cundh2o said, it's DBMS-specific. But you can subclass the Order class and define your own custom ordering. For example, for SQL Server: public class RandomOrder: Order { public RandomOrder() : base("", true) {} public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { return new SqlString("newid()"); } } 回答2: If you are not limited to using ICriteria, I might recommend using HQL instead

NHibernate How do I query against an IList<string> property?

坚强是说给别人听的谎言 提交于 2019-12-17 06:50:27
问题 I am trying to query against an IList<string> property on one of my domain classes using NHibernate. Here is a simple example to demonstrate: public class Demo { public Demo() { this.Tags = new List<string>(); } public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList<string> Tags { get; set; } } Mapped like this: <class name="Demo"> <id name="Id" /> <property name="Name" /> <bag name="Tags"> <key column="DemoId"/> <element column="Tag" type="String" /

nHibernate Criteria API Projections

北城余情 提交于 2019-12-11 19:15:15
问题 I have an entity that is like this public class Customer { public Customer() { Addresses = new List<Address>(); } public int CustomerId { get; set; } public string Name { get; set; } public IList<Address> Addresses { get; set; } } And I am trying to query it using the Criteria API like this. ICriteria query = m_CustomerRepository.Query() .CreateAlias("Address", "a", NHibernate.SqlCommand.JoinType.LeftOuterJoin); var result = query .SetProjection(Projections.Distinct( Projections