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

  • Home
  • SEARCH
  • 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 7786883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:30:05+00:00 2026-06-01T20:30:05+00:00

This is my first post so please bear with me. I’m trying to execute

  • 0

This is my first post so please bear with me. I’m trying to execute a HQL query from within a method using HibernateTemplate. The reason I’m doing this way is because the actual table has many more columns but I’m only interested in only two. The result of the query is used in performing updateOrSave operation later.

So the class structure looks like:

class XYZDAO {
   ...
   ...

   public void createReview(....) {
      ...
      ...
      class Temp {
        private final float rating;
        private final int count;

        @SuppressWarnings("unused")
        public Temp(float rating, int count)
        {
            this.rating = rating;
            this.count = count;
        }
        public float getRating()
        {
            return rating;
        }
        public int getCount()
        {
            return count;
        }          
      }
      List<Temp> avgRatingWrapper = getHibernateTemplate().find("SELECT new Temp(AVG(RATING), COUNT(*)) FROM RATINGS WHERE ADVENTURE_ID = ?", Integer.parseInt(adventureId));
      ...
      ...
   }
}

When I run the code following exception occurs:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: RATINGS is not mapped [SELECT new Temp(AVG(RATING), COUNT(*)) FROM RATINGS WHERE ADVENTURE_ID = ?]

I already have a full blown mapping hibernate mapping for the RATINGS table:

<hibernate-mapping>
    <class name="com.xyz.abc.dao.hibernate.Ratings" table="ADV_ADMN.RATINGS">
        <id name="id" type="int" column="ID">
            <generator class="seqhilo">
                <param name="sequence">ADV_ADMN.RATINGS_ID_SEQ</param>
                <param name="allocationSize">1</param>
            </generator>
        </id>
        <version name="timestamp" type="timestamp">
            <column name="TIMESTAMP" length="19" not-null="true" />
        </version>
        <property name="adventureId" type="int">
            <column name="ADVENTURE_ID" not-null="true" />
        </property>
        <property name="reviewer" type="string">
            <column name="REVIEWER" length="45" not-null="true" />
        </property>
        <property name="rating" type="java.lang.Float">
            <column name="RATING" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

Now i understand that I need to perform some kind of mapping either in the hibernate.hbm.xml files or provide annotation for the Temp class to map the RATINGS table. I was wondering if there is any other way to get around the problem. I figured if you use Session.createSqlQuery(….) then you can add entities to it which can circumvent the problem. But I’m not sure is there is a way to do that in HibernateTemplate.

Any help/pointers are greatly appreciated.

  • 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-01T20:30:06+00:00Added an answer on June 1, 2026 at 8:30 pm

    Code it’s not tested, but I think you mean something like this. Create the SQLQUery, and define the scalars as Strings, and use the getHibernateTemplate().execute() function.

    return getHibernateTemplate().execute(new HibernateCallback<List>() {
    
            public String doInHibernate(Session s)
                    throws HibernateException, SQLException {
                SQLQuery sql=s.createSQLQuery("SELECT AVG(RATING) as r, COUNT(*) as c FROM RATINGS WHERE ADVENTURE_ID = ?");
                    sql.setParameter(0, adventureId);
                sql.addScalar("r");
                sql.addScalar("c");
                return sql.list();
            }
        });
    

    If it’s HQL and getHibernateTemplate().find what you want to use, then use “select … from com.xyz.abc.dao.hibernate.Ratings”

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

Sidebar

Related Questions

This is my first post on Stack, so please bear with me if I
This is my first post in any forum so please bear with me. I
This is my first post on StackOverflow, so please bear with me. And I
This is my first post, so please bear with me. I'll try and be
This is my first post here and I wanted to get some input from
This is my first post, so please be nice :) I have a question
This is my first post, and first please forgive me for my poor english.
This is my first post, so please forgive me if this question has been
This is my first post, so please be gentle. I've been playing around with
This is my first post on this forum (so please be gentle ) I'm

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.