criteria-api

Query many-to-many without selecting all objects using Criteria API

I have a many-to-many relationship between Project and Site. I am trying to retrieve a list of Sites for a project using the Criteria API. I've got this working but the query also selects all of the columns for the associated Projects, which I don't want. I wrote what I thought was an equivalent query using HQL and it only selects the Si...

JPA-2.0 Simple Select-Where question

I am stuck with a problem concerning JPA-2.0 queries with relationships. How would it be possible to select any Dataset with at least one Event with type = B? @Entity class Dataset { @OneToMany(fetch = FetchType.LAZY, mappedBy = "dataset") public List<Event> events; } @Entity class Event { @ManyToOne @JoinColumn pub...

Using JPA2 criteria API without Metamodel on a List property

How can I formulate the following JPA2 criteria query without using the metamodel classes: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> cq = cb.createQuery(Employee.class); Root<Employee> emp = cq.from(Employee.class); cq.where(cb.isEmpty(emp.get(Employee_.projects))); cq.select(emp); I wou...

Sorting two columns using NHibernate Criteria API

I need to sort a result set by two columns, one of which is always NULL. A simplified view of Database where the columns I wish to sort on look like : ColumnA is from Table A Column B, B1 is from table B ColumnA ColumnB ColumnB1 U NULL NULL P NULL NULL L NULL NULL NULL U NULL NULL ...

Hibernate Criteria Projection

Well as the question says, i am trying to make a projection criteria querying only couple of the table attributes. So I have a Person Table/class and it has about 40 attributes i want my criteria to get dynamical number of attributes, lets say 10, 11 or 12.(sql terms "select firstname, lastname from person") and i was doing it like this ...

Hibernate Many-to-Many Criteria Projection

EDIT> i am at a dead end... so i can continue looking for the main reason .. Please tell me how to make a simple criteria for many to many relationships which has more than one eq restrictions, for an example, how to get the person speaking eng & german in the example shown here... My situation is like this i have two classes person and...

JPA 2 -- Using @ElementCollection in CriteriaQuery

@Entity public class Person { @ElementCollection private List<Location> locations; [...] } @Embeddable public class Location { private Integer dummy; private Date creationDate; [...] } Given the following structure, I'd like to perform the HQL or CriteriaQu...

How do I query by the count of a property in nhibernate without using a detached criteria?

hey guys, I'm using NHibernate version 2.1.2.4000. The Entity class bowl { int id { get; set; } List<fruit> fruits { get; set; } } The Desired (pseudo) Query var bowls = repository.where(b => b.fruits.count > 1); The Question How do I do the above query using the NHibernate criteria API? Ideally I'd like to be able to do...

Hibernate - Criteria - bidirectional search with unidirectional mapping?

Could you tell me is possible to search by hibernate criteria on bidirectional direction with only unidirectional associations? For examle: I have two class: Box { Long id; Set parcels; Integer status; // other properites } Parcel { Long id; // other properties } I've got associations one-to-many in cl...

Hibernate simple criteria query solving problem

I'm stuck with a very simple criteria query problem: sess .createCriteria(user.class, "user") .user_c.add(Restrictions.eq("user.status", 1)) .user_c.createAlias("user.userCategories","ucs") .add(Restrictions.eq("ucs.category_id",1)); .add(Restrictions.eq("ucs.category_id",'yes')); .add(Restrict...

Restricting an NHibernate query using ICriteria according to an enumeration of enums

I have an entity, with a field of type enum, that is persisted as an integer in my database. When retrieving objects from the database using ICriteria, I wish to restrict the results to those with the field being a member of a collection of enum values. Does Restrictions.In work with a collection of enums? The following does not work. ...

JPA 2 + Criteria API + get oldest element in subquery

I have two entities (Dataset and Item), where the first one contains a list of the latter one. @Entity class Dataset { @Id long id; List<Item> items; } @Entity class Item { @Id long id; @Temporal Date date; String someCriteria; } I am now looking for any dataset, which references an item with someCriteria = 'x' as...

org.hibernate.QueryException: Not all named parameters have been set

Hi all, I'm getting extremely weird behavior out of JPA 2.0 Criteria API with Hibernate 3.5.1-Final as a provider. I'm trying to build a dynamic query that looks like this in JPQL: SELECT e FROM Employee e WHERE lower(e.firstName) like lower(:employeeName) OR lower(e.lastName) like lower(:employeeName) but keep getting the er...

JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException

I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy: ... Root<JPAAlbum> albm = cq.from(JPAAlbum.class); ... albm.get(JPAAlbum_.theme) ... ; but this Root.get always throws a NullPointerException. JPAAlbum_.theme was automatically generated by Hibernate and looks like public static volatile Sing...

What to use: JPQL or Criteria API?

My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API? ...

JPA criteria builder with multiple joins

hi, i'm struggling to create a jpa query that makes use of several tables. i cannot seem to understand how to join the tables together. this is the query i am trying to create: SELECT algm.m_l_i, algnsm.n_s_i FROM algm, alg, algnsm, mal WHERE algm.l_g_i = alg.l_g_i AND alg.l_g_t = 'xxx' AND algnsm.l_g_i = algm.l_g_i AND mal....

Composite Id's and Restrictions.IdEq or with comparison in Linq not working as expected

I have an entity where a composite id is used. I changed to code to make use of wrapping the composite id in a seperate key class. I expected that with Linq I could do a comparison on key object and with the Criteria API to use Restrictions.IdEq but both fail. I need to explicitly compare the key values to make it work. I cannot find an...

JPA/Metamodel: Strange (inconsistent ?) example in Sun Docs

In Sun Online resources, they provide son example about the usage of the Criteria/Metamodel API, but as far as I understand Java, it seems to be impossible to work: CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Metamodel m = em.getMetamodel(); EntityType<Pet> Pet_ = m.entity(Pet.class); EntityType<Owner> Owner_ = m.entity(Owner.cla...

Using @ElementCollection in CriteriaQuery

public enum ReportStatus { SUCCCEED, FAILED; } public class Work { @ElementCollection @Enumerated(EnumType.STRING) List<ReportStatus> reportStatuses; } Given the following structure, I'd like to perform a query to find all the work filtered by reportStatuses. It works fine with the following hql syntax : public List<...

NHibernate Criteria API - order by max of two properties

Hi, I have a PrivateMessage class and I want to get list of PMs for user sorted chronologically either by CreationDate or LastAnswerDate (depending on which is more recent) using Criteria API. How to sort by max of these two properies in Criteria API? My code looks similar to following: var dc = DetachedCriteria.For<PrivateMessage>(); ...