criteria-api

setfirstresult & setmaxresult in child collection

I have and entity lets call it Entity, and a Child collection Children. I have a screen where the user has the Entity information, and a list with the Children collection, but that collection can be get very big, so i was thinking about using paging: get the first 20 elements, and lazy load the next only if the user explicitly presses t...

Select columns from join table only without requiring a join

Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key (Id) ) create table Items ( Id INT IDENTITY NOT NULL, primary key (Id) ) create table OrdersItems ( OrderId INT not null, ItemId INT not null, primary key (OrderId, ItemId) ) Is it possible to use HQL/criteria API to contruct a query...

Dynamic JPA 2.0 query using Criteria API

Hello all, I am a bit stucked constructing a dynamic query using the CriteriaBuilder of JPA 2.0. I have quite a common use case I guess: User supplies a arbitrary amount of search parameters X to be and / or concatenated: like : select e from Foo where (name = X1 or name = X2 .. or name = Xn ) The Method or of CriteriaBuilder is no...

How to order records by a computed column in Criteria API?

How can I write the below HQL query using the Criteria API? select s.Name, sum(q.PointsObtained), sum(q.TotalPoints) from Student s join s.Quizzes q group by s.Name order by (sum(q.PointsObtained) / sum(q.TotalPoints)) desc ...

Example using countDistinct in a JPA Criteria API query

I'm having trouble figuring out how to represent the following JPQL query: SELECT count(e) FROM Foo e using Criteria API. What I'm trying is: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Foo> c = cb.createQuery(Foo.class); Root<Foo> f = c.from(Foo.class); c.select(cb.count(f)); but this is not working. I also tried: ...

Creating queries using Criteria API (JPA 2.0)

Hello there ! I'm trying to create a query with the Criteria API from JPA 2.0, but I can't make it work. The problem is with the "between" conditional method. I read some documentation to know how I have to do it, but since I'm discovering JPA, I don't understand why it does not work. First, I can't see "creationDate" which should app...

Ordering the results of a Hibernate Criteria query by using information of the child entities of the asked entities

I have got two entities Person and Book. Only one instance of a specific book is stored to the system (When a book is added, application checks if that book is already found before adding a new row to the database). Relevant source code of the entities is can be found below: @Entity @Table(name="persons") @SequenceGenerator(name="id_seq...

Problem with NHibernate

I am trying to get a list of Products that share the Category. NHibernate returns no product which is wrong. Here is my Criteria API method : public IList<Product> GetProductForCategory(string name) { return _session.CreateCriteria(typeof(Product)) .CreateCriteria("Categories") .Ad...

Is there something like Restrictions.eq(true, false) in Criteria API ?

I need a generic Criterion which forces the result to zero matches. Something like Restrictions.eq(true, false) ? ...

"could not resolve property" exception while adding criteria for DateTime property

public class Entity { public virtual string Title { get; set; } public virtual DateTime CreationDate { get; set; } public virtual int Count { get; set; } } I have a problem when creating Criteria for a given entity: var s = session.CreateCriteria<Entity>() .Add(Expression.Ge("CreationD...

Hibernate Criteria API - adding a criterion: string should be in collection

I have to following entity object @Entity public class Foobar { ... private List<String> uuids; ... } Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just not sure how to make the appropriate criterion. ...

How to retrieve row count of one-to-many relation while also including original entity?

Say I have two entities Foo and Bar where Foo has-many Bar's, class Foo { int ImportantNumber { get; set; } IEnumerable<Bar> Bars { get; set; } } class FooDTO { Foo Foo { get; set; } int BarCount { get; set; } } How can I efficiently sum up the number of Bars per Foo in a DTO using a single query, preferrably only with the Cr...

Load collections eagerly in NHibernate using Criteria API

I have an entity A which HasMany entities B and entities C. All entities A, B and C have some references x,y and z which should be loaded eagerly. I want to read from the database all entities A, and load the collections of B and C eagerly using criteria API. So far, I am able to fetch the references in 'A' eagerly. But when the collec...

Can the same CriteriaBuilder (JPA 2) instance be used to create multiple queries?

This seems like a pretty simple question, but I have not managed to find a definitive answer yet. I have a DAO class, which is naturally querying the database by using criteria queries. So I would like to know if it is safe to use the same CriteriaBuilder implementation for the creation of different queries or do I have to create new Cri...

Nhibernate join on a table twice

Consider the following Class structure... public class ListViewControl { public int SystemId {get; set;} public List<ControlAction> Actions {get; set;} public List<ControlAction> ListViewActions {get; set;} } public class ControlAction { public string blahBlah {get; set;} } I want to load class ListViewControl eagerly...

Basic date/time manipulation in NHiberate query

I'm trying to restrict my NHibernate query with some basic date/time manipulation. More specifically, I want to execute the following statement (pseudo-SQL): select * from article where created_on + lifespan >= sysdate with: created_on is mapped to a property of type DateTime. lifespan is mapped to a property of type TimeSpan. sysda...

NHibernate Query

Is it possible to get NHibernate to generate a query similar to the following with HQL or Criteria API? select * from ( select row_number() over ( partition by Column1 order by Column2 ) as RowNumber, T.* from MyTable T ) where RowNumber = 1 I can get it to execute the inner select using the formu...

Hibernate Criteria API: count and group by with result in Map

The Car entity is mapped to a database table with 2 columns: ID and Color. CarDao has the following method: Map<Color, Integer> countByColor(); If we have 3 red cars and 2 blue cars in the database table, the method returns a map with 2 keys (red and blue) and the corresponding count (3 resp. 2). I would like to do this with the Cri...

NHibernate: Filtering a child collection which includes a join

I have the following mapping files: <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namespace="EntityNamespace"> <class name="Server" xmlns="urn:nhibernate-mapping-2.2" table="tblServer" > <id name="Id" type="int"> <generator class="native"...

Hibernate - Restriction for class in a list

I'm trying to pull back a list of items that have a specific type of item in a set. For example: <class name="Owner" table="OWNER"> <id name="id" column="OWNER_ID" /> <set name="cats" table="OWNER_CATS" lazy="false"> <key column="OWNER_ID" /> <many-to-many class="Cat" /> </set> <class name="Cat" table="CAT" discriminator-v...