criteria-api

NHibernate - CreateCriteria vs CreateAlias

Hi, Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing". I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the proper...

NHibernate - easiest way to do a LIKE search against an integer column with Criteria API?

I'm trying to do a like search against an integer column, what I need to do is actually cast the column to a varchar and then do the like search. Is this possible? what's the easiest way to do this using the Criteria API? var search = "123"; criteria.Add(Restrictions.Like("Number", "%" + search + "%")) ...

Hibernate Criteria API - how to order by collection size?

Assuming I have classes User and UserGroup. There is an optional 1-many group to user association and the association is mapped from both sides (the UserGroup side via a property called "members", which is a HashSet, the User side via property "group"). Using the Criteria API, how can I query for all groups, sorted by count of group mem...

Does Hibernate's Criteria API still not support nested relations

I'd like to use Hibernate's Criteria API for precisely what everybody says is probably its most likely use case, applying complex search criteria. Problem is, the table that I want to query against is not composed entirely of primitive values, but partially from other objects, and I need to query against those object's id's. I found th...

NHibernate query for object with child collection property

Hi, I need help with an nhibernate query. I would prefer to use Criteria API if possible, otherwise HQL is ok. I have an Employee object with an Account object property, the Account has a collection of Entry objects, and each Entry has an Amount property. (see class diagram). http://www.threeboxes.com.au/employeeQuery1.jpg I need a q...

Using NHibernate Criteria API to select sepcific set of data together with a count

I have the following domain set up for persistence with NHibernate: I am using the PaperConfiguration as the root aggregate. I want to select all PaperConfiguration objects for a given Tier and AcademicYearConfiguration. This works really well as per the following example: ICriteria criteria = session.CreateCriteria<PaperConfiguratio...

Hibernate criteria query on different properties of different objects

Suppose I have classes like: class A { B getB(); C getC(); } class B { String getFoo(); } class C { int getBar(); } and I want to filter criteria on A, two filters on different subclass properties, like: Criteria criteriaA = session.createCriteria(A.class); Criteria criteriaB = criteriaA.createCriteria("b").add(Restrictions.eq(...

NHibernate Criteria using Projections for Substring with in clause

I had a scenario in Oracle where i neeed to match a substring part of column with a list of values. i was using sqlfunction projection for applying the substring on the required column , and addeed that projection as part of an In Clause Restriction. Below is the simplified criteria i wrote for that. ICriteria criteriaQuery = session.C...

Using Hibernate Criteria API To Query Many-To-One Relationships with Projections

Hi I am trying to use Criteria API in following scenario: I have two tables, Schedule and Route (with their classes and mappings). Route has many-to-one relationship with Schedule. Route has an integer property sequence. Now I need to fetch all those Schedule objects whose associated Route objects fulfill the following condition: r...

Getting index of a row in a list

Hi, I have a paging method that uses the criteria API's SetMaxResults and SetFirstResult. Functionally, it works fine. What I'd like to do now is provide another method that retrieves the index of a given item within the set of all items, thus allowing me to calculate what page that item is in. I am able to calculate the index correct...

NHibernate: How to exclude a class which is part of a join using the Criteria API

I am still new to Hibernate and am attempting to use it for a web site I have inherited. Unfortunately that means sometimes the db schemas don't always make sense. With that said, I am trying to build the following HQL query using the Criteria API from TableB b where b.id = :id and b.TableAProperty.UserId = :userId The above HQL sta...

Converting a HQL-query which includes elements() to Criteria API

I'm having trouble converting the following HQL-query to Criteria API and I was wondering if I could get some help from you guys SELECT child FROM Foo AS child WHERE child IN (SELECT elements(obj.foos) FROM Bar AS obj WHERE obj.id = ?) In other words, I want to fetch all Foos that Bar references to in the instance of Bar whose id ...

Criteria API returns a too small resultset

How is this possible, I have to following criteria Criteria criteria = getSession().createCriteria(c); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.add(Restrictions.eq("active",true)); List list = criteria.list(); The size of list is now 20. If I add a max results to the criteria, Criteria criteria = getSes...

(Lazy) LEFT OUTER JOIN using the Hibernate Criteria API

Hi all, I want to perform a LEFT OUTER JOIN between two tables using the Criteria API. All I could find in the Hibernate documentation is this method: Criteria criteria = this.crudService .initializeCriteria(Applicant.class) .setFetchMode("products", FetchMode.JOIN) .createAlias("products", "product"...

Hibernate Criteria: Left Outer Join with restrictions on both tables

Hi all, I am doing a LEFT OUTER JOIN, but I am only able to apply Restrictions on the first table. Is there a way ti apply on the second table as well? Here is my code: Criteria criteria = this.crudService .initializeCriteria(Applicant.class).setFetchMode("products", FetchMode.JOIN);. This works (applicant has a...

NHibernate projection: How to get a typed type using the Criteria API with projection

List<object[]> products = GetSession().CreateCriteria<Product>() .SetProjection(Projections.ProjectionList() .Add(Projections.Property("Id")) .Add(Projections.Property("Name")) .Add(Projections.Property("Price")) ) ...

Hibernate Criteria API and Scalar Subqueries

Hi, I want to translate a HQL query to Criteria API but I don't know if it's possible to write the same thing with Criteria. The HQL looks like this: select distinct new DataTransferObject(property1, property2, ..., (select NVL(property3, null) from Table1 where property3 in elements(...) and ... ), property4, ..., (select .....), ...)...

Projections.count() and Projections.countDistinct() both result in the same query

EDIT: I've edited this post completely, so that the new description of my problem includes all the details and not only what I previously considered relevant. Maybe this new description will help to solve the problem I'm facing. I have two entity classes, Customer and CustomerGroup. The relation between customer and customer groups is M...

Criteria SpatialRestrictions.IsWithinDistance NHibernate.Spatial

Has anyone implemented this, or know if it would be difficult to implement this/have any pointers? public static SpatialRelationCriterion IsWithinDistance(string propertyName, object anotherGeometry, double distance) { // TODO: Implement throw new NotImplementedException(); } from NHibernate.Spatial.Criterion.SpatialRestrictio...

How can you remove a criterion from criteria?

Hello, For instance if I do something like: Criteria c = session.createCriteria(Book.class) .add(Expression.ge("release",reDate); .add(Expression.ge("price",price); .addOrder( Order.asc("date") ) .setFirstResult(0) .setMaxResults(10); c.list(); How can I use the same cr...