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 7580021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:48:48+00:00 2026-05-30T17:48:48+00:00

This is my simple JPQL: SELECT s FROM Site s GROUP BY s.siteType siteResult

  • 0

This is my simple JPQL:

SELECT s
FROM Site s
GROUP BY s.siteType

siteResult = q.getResultList();
for (Site site : siteResult) {
    // loops all sites
}

This query returns all sites, including sites of the same siteType.
I’m using JPA 2.0 Eclipselink.
Whats wrong here?

  • 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-05-30T17:48:49+00:00Added an answer on May 30, 2026 at 5:48 pm

    Such a query does not make sense. If you use GROUP BY, other attributes in SELECT should be aggregated. As it is said in JPA specification:

    The requirements for the SELECT clause when GROUP BY is used follow
    those of SQL: namely, any item that appears in the SELECT clause
    (other than as an aggregate function or as an argument to an aggregate
    function) must also appear in the GROUP BY clause. In forming the
    groups, null values are treated as the same for grouping purposes.

    If you think SQL counterpart of your query:

    SELECT s.attr1, attr2, s.siteType
    FROM site s
    GROUP BY (s.siteType)
    

    you notice that it is hard to imagine which possible value of attr1 and attr2 should be chosen.

    In such a case EclipseLink with derby just drops GROUP BY away from the query, which is of course little bit questionable way to handle invalid JPQL. I like more how Hibernate+MySQL behaves with such a invalid JPQL, it fails with quite clear error message:

    java.sql.SQLSyntaxErrorException: The SELECT list of a grouped query
    contains at least one invalid expression. If a SELECT list has a GROUP
    BY, the list may only contain valid grouping expressions and valid
    aggregate expressions.

    Answer to comment:
    One Site contains probably also attributes other than siteType as well. Lets use following example:

    public class Site {
        int id; 
        String siteType;
    } 
    

    and two instances: (id=1, siteType=”same”), (id=2, siteType=”same”). Now when type of select is Site itself (or all attributes of it) and you make group by by siteType, it is impossible to define should result have one with id value 1 or 2. Thats why you have to use some aggregate function (like AVG, which gives you average of attribute values) for remaining attributes (id in our case).

    Behind this link: ObjectDB GROUP BY you can find some examples with GROUP BY and aggregates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This simple query: SELECT trip_id from stop_times WHERE stop_id = 345 Takes about 80ms,
This simple query session = com.jthink.songlayer.hibernate.HibernateUtil.getSession(); Query q = session.createQuery(recNo from SongChanges); giving this
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar
This simple Linq query: from c in mycontext.Customers join o in mycontext.Orders on c.CustomerId
This simple regex matching returns a string instead of an object on every browser
This simple query throws the Ambiguous column name TaskID error on one db-server only
This simple Perl script is translating stories from a database into XML, but this
this simple code is causing the sp to not compile with all sorts of
In this simple sparql query I get a list of subjects whose object is
This simple piece of sql works perfect in sql server... Select it.Id as Id,

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.