nhibernate-criteria

Map Entities to self-joined Table by Id

≯℡__Kan透↙ 提交于 2020-01-05 05:48:04
问题 My legacy table "AllData" has those columns: Id, Title, LookupColumn1Id My entities: public class BaseEntity { public virtual int Id { get; set; } public virtual string Name { get; set; } } public class Employee: BaseEntity { public virtual int DepartmentId { get; set; } public virtual string DepartmentName { get; set; } } public class Department: BaseEntity { public virtual int HeadManagerId { get; set; } } I want to generate SELECT like this: SELECT EmployeeTable.Title, DepartmentTable.Id,

Nhibernate migrate ICriteria to QueryOver

时光毁灭记忆、已成空白 提交于 2020-01-05 05:41:47
问题 We are using ICriteria and now we would like to switch to more readable QueryOver in Nhibernate Can someone give me a hint how to convert this generic pagination logic for Icriteria to QueryOver? public static PagedList<T> PagedList<T>(this ICriteria criteria, ISession session, int pageIndex, int pageSize) where T : class { if (pageIndex < 0) pageIndex = 0; var countCrit = (ICriteria)criteria.Clone(); countCrit.ClearOrders(); // so we don’t have missing group by exceptions var results =

Hibernate Criteria: Projecting Count with group by clause

坚强是说给别人听的谎言 提交于 2020-01-04 02:39:33
问题 I want to execute the following SQL select count(*) as myCount from user group by name; I came up with the following criteria for the same DetachedCriteria.ForClass(typeof(UserDTO)) .setProjections(Projections.ProjectionList() .Add(Projections.rowCount(),"myCount") .Add(Projections.groupProperty("this.name")); I get the result back as pair of the count and name,How can I get just the count from this. 回答1: I don't think you can do it with Criteria, but it's easy with HQL. It's exactly the same

NHibernate - Wrong Columns on Queries

北慕城南 提交于 2019-12-31 22:21:36
问题 I'm getting an intermittant problem with NHibernate where it generates a query for an entity, but replaces one of the columns with a column from completely different (and unrelated) entity. It only ever replaces a single column, and is generally solved by restarting the application (though sometimes it takes a couple of attempts). ASP.NET application (.NET 4.0) SessionFactory created during Application_Start NHibernate 3.3.1- All mappings/configuration done via Mapping By Code Using

NHibernate COALESCE issue

久未见 提交于 2019-12-23 21:29:00
问题 I am trying to express the following SQL query with NHibernate DECLARE @date DATETIME = NULL; SELECT ER.Id , ER.DocumentDate FROM ExpenseReport ER WHERE ER.PeriodFrom >= COALESCE(@date, ER.PeriodFrom) OR ER.PeriodTo <= COALESCE(@date, ER.PeriodTo); So, in the C# part I do have the following classes: for the entity : ExpenseReport for my search itself a separate class Code snippets: // ----- Entity class. public partial class ExpenseReport { public Nullable<System.DateTime> PeriodFrom { get;

Loading multi-level collections without duplicates in NHibernate

心不动则不痛 提交于 2019-12-23 19:13:22
问题 My question is very similar to this one (that wasn't really answered): Nhibernate: distinct results in second level Collection I have this object model: class EntityA { ... IList<EntityB> BList { get; protected set; } ... } class EntityB { ... does NOT reference its parent EntityA... IList<EntityC> CList { get; protected set; } } They are One-to-Many relations. EntityB and C do not have an object reference to its parent object. I'd like to fully load the collections by performing something

Union with NHibernate and Criteria?

瘦欲@ 提交于 2019-12-23 02:59:09
问题 Union with NHibernate and Criteria: Is it possible in Criteria or QueryOver? If not, is there any other way to achieve a union of two result within the same query? 回答1: You can't do a union directly, but you can do two future queries and union the results in code: var resultSet1 = this.Session.CreateCriteria<A>().Future<A>(); var resultSet2 = this.Session.CreateCriteria<B>().Future<B>(); After this, when either result set is enumerated, NHibernate will issue a single query to the database

Fluent NHibernate and filtering one-to-many relationship on query requiring multiple joins?

安稳与你 提交于 2019-12-21 20:48:47
问题 I recently got started with NHibernate and am having some trouble implementing the domain model outlined further down. What I'm looking for is a way to filter the relationship between an Item and it's ItemData collection on specific DataStores. DataStores are either global, in which case they are always returned, or specific to a user identity (based on application instance). In SQL this can be accomplished using a simple query: SELECT * FROM Items i INNER JOIN ItemData id ON (i.ItemId=id

Querying with NHibernate

好久不见. 提交于 2019-12-20 10:46:56
问题 I am new to NHibernate and I am trying to learn how to query my data. Below is the configuration xml. Only the recipe is shown. I want to be able to query recipes by recipetitle from keywords entered and also ingredients from ingredientname. So you might enter "pasta wine" for example. This is what I have tried but gives me an error. hql = "from Recipe r " + "left join r.Images " + "inner join r.User " + "inner join r.Ingredients i " + "where i.IngredientName Like '%pasta%' OR i

Make a SQL Query to Nhibernate

跟風遠走 提交于 2019-12-20 07:51:08
问题 How do I get this SQL query to Nhibernate? SELECT Customer.name FROM Company INNER JOIN Customer ON Company.CompanyId = Customer.CompanyId where CompanyId = 2 回答1: If you are familiar with LINQ it is very very simple, you have to directly access the reference filed just as an entity. and you will get the properties of that entity and so on you can access till the nth node. Nhibernate will take care of reference fields as entities.. //Here is the sample this may work //CustomerList is a