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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:40:23+00:00 2026-05-26T11:40:23+00:00

I have a table fruit with four columns id, name, color, shape. entries in

  • 0

I have a table fruit with four columns id, name, color, shape.

entries in the table would be:

1, apple, red, round
2, banana, yellow, long
3, tomato, red, round
4, orange, orange, round

Now I made an entity class Fruit mapped to above table.

@Entity
@Table(name="fruit")
public class Fruit implements Serializable {

@Id
@Column(name="ID")
String id;

@Column(name="NAME")
String name;

@Column(name="COLOR")
String color;

@Column(name="SHAPE")
String shape;

//getters/setters goes here
}

In my DAO class, the code is:

String myQuery = "Select f.shape, f.name from Fruit f where f.shape = :shape";
Query query = this.em.createQuery(myQuery);
query.setParameter("shape", "round");

As obvious, running above query will return 3 rows.

I have a simple TO class FruitSearchTO

class FruitSearchTO
{
  String shape;
  String name;

  //getters/setters here
}

This TO complies with the rows returned by my query.

But in my DAO running something like:

List<FruitSearchTO> fruitList = new ArrayList<FruitSearchTO>();  
fruitList = query.getResultList();

is throwing exception java.lang.ClassCastException: [Ljava.lang.Object; incompatible with FruitSearchTO]

Where am I going wrong and what is the solution to this ?

  • 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-26T11:40:24+00:00Added an answer on May 26, 2026 at 11:40 am

    The HQL you’re using will return a List<Object[]>, each element of the List being an array with shape in position 0 and name in position 1.

    You can make the HQL return a List<FruitSearchTO> using an AliasToBeanResultTransformer:

    List fruitList = s.createQuery(
      "select f.shape as shape, f.name as name from Fruit f where f.shape = :shape;")
      .setParameter("shape", paramShape)
      .setResultTransformer( Transformers.aliasToBean(FruitSearchTO.class))
      .list();
    FruitSearchTOdto = (FruitSearchTO) fruitList .get(0);
    

    Alternatively, if FruitSearchTO has an appropriate constructor:, you can also achieve this with select new FruitSearchTO(f.shape, f.name).

    Take a look at the Hibernate Reference chapter on HQL, particularly 15.6 The select clause chapter.

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

Sidebar

Related Questions

Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have a table with 3 fields: color, fruit, date. I can pick 1
I have table with 50 entries (users with such details like Name Surname Location
I have a table lets say called FavoriteFruits that has NAME , FRUIT ,
I have table in SQL Server 2005 that contains two columns Fruit and Colour
I have a table called baskets with these columns: basket (name of the basket)
I have table with 3 columns A B C. I want to select *
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table like this table name: products +----+-------------+-----------+ | id | name
Conceptually, I have a table with a list of fruit items. I want, in

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.