DetachedCriteria « Query « JPA Q&A

1. Hibernate DetachedCriteria in FROM clause

I have 2 tables:

    orders: id
    items: id, orderId, total, flag
I would like to make following query using Hibernate Criteria (DetachedCriteria):

2. How do i create a Hibernate Criteria to order by some properties of collection

Say, i have an entity that has a history of operations as a collection. I want to sort entities by the date of the latest operation (it's the first element of ...

3. Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria

I'm running into an issue with adding JOIN's to a subquery using DetachedCriteria. The code looks roughly like this:

Criteria criteria = createCacheableCriteria(ProductLine.class, "productLine");
criteria.add(Expression.eq("", "Y"));

DetachedCriteria subCriteria = DetachedCriteria.forClass(Model.class, "model");
subCriteria.createAlias("model.modelLanguages", "modelLang");
subCriteria.createAlias("modelLang.language", "lang");
criteria.add(Expression.eq("lang.langCode", "EN"));
criteria.add(, ...

4. Hibernate Subquery and DetachedCriteria

I have created a DetachedCriteria that is retrieving estates that have the isApproved and isPublished set to true. It is defined in this way:

DetachedCriteria activePublishedCriteria = DetachedCriteria.forClass(Estate.class)
    .add(Restrictions.eq("isApproved", ...

5. Criteria query whith FROM clause which contains only one query and does not contain any tables

I have the following query and I don't know how can I write it using criteria.

SELECT questions_cnt, COUNT(*) users_cnt
    SELECT COUNT(DISTINCT question) questions_cnt

6. Hibernate Query DetachedCriteria VS bulkUpdate SQL using Collection with "in" Calause Problem

I facing a problem on how to use SQL "in" in bulkUpdate Hibernate. Example : Country is an enum type.

Normal SQL statement

Update Persons SET country = Country.China
WHERE country IN (Country.HK, Country.Taiwan);
Hibernate ...

7. Hibernate select groupProperty , rowCount with rowCount > n?

Sorry if it is a dumb question but I've stuck with this problem for a whole afternoon but cannot find a solution because I'm not skilled with complicated SQL : I want ...

8. How do I get hql string from DetachedCriteria

I'd like to get hql request from DetachedCriteria. Is there any right way of doing it(I mean toString is not right)?

9. Using Hibernate DetachedCriteria for calling aggregate functions

I have a DetachedCriteria which I am using to search a table based on a name field. I want to make the search case-insensitive, and am wondering if there is a ...

10. Hibernate criteria: why no Subqueries.isNull(DetachedCriteria)?

I want to translate a script like this into criteria:

  A.some_date > (select bdate from B where ...)
  OR (select bdate from B where ...) IS ...

11. Hibernate Criteria: performing left outer join in Subquery and DetachedCriteria

I have three entities

@Table(name = "PRODUCT")
public abstract class ProductVO implements Comparable<ProductVO> {

@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private Integer someId;

@ManyToMany(targetEntity = DeliveryMethodVO.class, cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Set<DeliveryMethodVO> methods = ...

12. How to write Hibernate query for collections Set

I have following Entity

public class Employee{

    private Integer id;
    private String name;
    private String address;
    private Date created;

13. About Distinct using DetachedCriteria in Hibernate

Can any solve this problem ? if possible plz give reply to me. The problem is as follows: I got a problem whenever i am using this query , DetachedCriteria query = DetachedCriteria.forClass(GoodsReturned.class) .add(Property.forName("returnedDate").between(cal1.getTime(), cal2.getTime())) .setProjection(Projections.distinct(Property.forName("GRTNo"))); List results = (getHibernateTemplate().findByCriteria(query)); // Upto here it is working fine for(GoodsReturned goodsReturned : results){ // But When iterating it is showing error the error ...

14. Get the total record count of an existing DetachedCriteria

ExtendedPaginatedList epl= paginatedListFactory.getPaginatedListFromRequest(request); . . . DetachedCriteria criteria = DetachedCriteria.forClass(MyClass.class); criteria.add(Restrictions.eq("id", "12"); ProjectionList projectList = Projections.projectionList(); projectList.add(Projections.groupProperty("foo")); projectList.add(Projections.groupProperty("bar")); projectList.add(Projections.groupProperty("baz")); ExtendedPaginatedList myList = dao.findByDetachedCriteria(epl, criteria); ...

15. DetachedCriteria : count before limitation?

Fearless, You may set the max results to be returned as 50. But it may not always cross 50 records it may also be less than 50. If you need only the count from the query, it is advisable to use count() instead returning all the results. As you said, you can also go with the size of the list.

19. DetachedCriteria SQL miss implicit join used as sub-select

DetachedCriteria dc = DetachedCriteria.forClass(ProjectTask.class, "task"). createAlias("project", "proj"). setProjection("")). ...

20. Problem with DetachedCriteria and Subqueries

Newbie Joined: Mon Jun 20, 2005 2:51 pm Posts: 13 Location: Brasil Hibernate version: 3.1.3 Please, I could use some help with DetachedCriteria and Subqueries. I would like to use Subqueries with the Criteria API, but I am getting a java.lang.ClassCastException: org.hibernate.impl.CriteriaImpl$Subcriteria. I have the following joined entities: User.hbm.xml: Main Table "users" Lernender.hbm.xml: secondary table: "lernender": linked to the first with ...

22. DetachedCriteria Query on Collection

Hi, I want to create a DetachedCriteria query using Hibernate3.1 the query is on collection of obects. For eg Class A { Collection b; // Collection of object B } Class B { String name; C c; // reference To class C } Class C { String cid; } The query is Search for A where childrens B are b1 AND/OR ...

23. Mix Named Query and DetachedCriteria

I just joined a team using Hibernate and we have a need to use a named query. The code already uses a DetachedCriteria to do all the filtering but I cannot find any examples of how to use it with a DetachedCriteria object. Can it be done? If so, how? Do I use DetachedCriteria.forEntityName()? I looked all over but cannot find ...

24. DetachedCriteria Projections and Outer joins

25. Using DetachedCriteria in Subqueries question.

Hibernate version: 3.2.3 I have User object that holds a set of Groups. I'm trying to create a criteria query that will provide me with a list of Groups the that any particular user doesn't belong to at the moment, so not in the set of groups on user. First I created a DetachedCriteria to be used as a subquery. DetachedCriteria ...

26. Sub-query with DetachedCriteria

I am trying to get hibernate to generate the following sql query: select count (*) from (select distinct msisdn, productid from PURCHASES) I've managed to generate the sub-query select distinct msisdn, productid from PURCHASES with the following code: criteria.setProjection(Projections.distinct(Projections.projectionList() .add("msisdn")) .add("productId")))) I've been looking at DetachedCriteria. Is this possible? I'm using hibernate 3.0.

27. DetachedCriteria and select disctinct

28. Hibernate DetachedCriteria query

Hello, I am new to Hibernate. We are using hibernate 3.0. I am getting InstantiationException. Here is my scenario: We are retrieving data using DetachedCriteria Query API. In the DetachedCriteria we are passing the name of the abstract class. The abstract class has two join sub classes which are again abstract class. If we pass the name of concrete class then ...

29. Proper Use Of Projection To Add A Subquery DetachedCriteria.

//Main query DetachedCriteria criteria = DetachedCriteria.forClass(E2KAccounts.class); criteria.add(Restrictions.eq(Constants.ACCOUNTTYPE, "GEN")); criteria.add(Restrictions.ilike(Constants.ACCOUNT_NUMBER, accountNumberPrefix + "%")); criteria.add(, currentDate)); criteria.addOrder(Order.asc(Constants.ACCOUNT_NUMBER)); ...

30. Show SQL Query from DetachedCriteria object