Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:21:16+00:00 2026-06-10T20:21:16+00:00

I have an SQL query that I would like to convert into a criteria

  • 0

I have an SQL query that I would like to convert into a criteria (Hibernate 3.3.2) to use it into my persistancy layer.
That’s the query :

select last_name
  from member
  where
    member.end_date > SYSDATE
  group by member.last_name
  having count(member.last_name) >1;

I already try a lot a combinations, but there is no result that would be good for me … This is my code :

Criteria criteria = getSession(false).createCriteria(Member.ENTITY_NAME);
criteria.setProjection(Projections.projectionList()
            .add(Projections.groupProperty("lastName"))
            .add(Projections.count("lastName").as("cptLastName"))
            );
criteria.add(Restrictions.ge("endDate", now))
            .add(Restrictions.le("startDate", now))
            //.add(Restrictions.gt("cptLastName", new Integer(1)))
            .addOrder(Order.asc("lastName"))
;

With writing it with the comment line, I have an objects list that contains the name and the number of times that the name appears in the database. But when i would like to decomment it, this the error :
java.sql.SQLException: ORA-00904: “Y1_”: invalid identifier

To develop this code, I am inspired by different post below:

  • Hibernate Criteria: Projecting Count with group by clause

  • Hibernate count rows with some criterias

  • How do we count rows using Hibernate?

  • HOW to use HAVING COUNT(*) with hibernate

  • Hibernate Criteria API – HAVING clause work arounds

I think I’m not far of the solution but is there anybody to help me?
If you have questions, don’t hesitate to ask me it.
And if you found another workaround for me to paste to this query, don’t hesitate to propose it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:21:18+00:00Added an answer on June 10, 2026 at 8:21 pm

    Hi if i am right you cant use the having clause count directly in hibernate criteria as of now, Please find below the modified query. If it doesnt works, please post the entire stack trace

    DetachedCriteria innerQuery = DetachedCriteria.forClass(Member.ENTITY_NAME, "inner");
      innerQuery.setProjection(Projections.rowCount());
      innerQuery.add(Restrictions.eqProperty("lastName", "outer.lastName"));
    
      Criteria c = s.createCriteria(Member.ENTITY_NAME, "outer");
      c.setProjection(Projections.property("lastName"));
      c.add(Restrictions.gt("endDate", now))   
      c.add(Subqueries.eq(new Integer(1), innerQuery));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat simple query in SQL that I would like to convert
Friends, I have a dynamic sql query that I would like to execute and
I have the following query in SQL which I would like to convert to
I have a result from an SQL query that I would like to sort
I have a SQL query that returns a Datatable: var routesTable = _dbhelper.Select(SELECT [RouteId],[UserId],[SourceName],[CreationTime]
We are using grails-groovy with Hibernate, I have a SQL query like this in
I would like to write a query on a sql 2008 that will report
I have a large (100+ tables) SQL Server 2005 database that I would like
I have this SQL Query that pulls data from 3 tables. I am unable
I have a SQL query that I'm trying to debug. It works fine for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.