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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:32:08+00:00 2026-05-22T16:32:08+00:00

Hibernate api doc introduces the Criteria and Projections classes, which can help select specified

  • 0

Hibernate api doc introduces the Criteria and Projections classes, which can help select specified fields of table and return an Object list.

BUT, i want to get an class A’s list (A is defined by myself), thus i can use it as usual object list.

for example,i have a class:

class A {
    String field1;
    String field2;
    String field3;

    String getField1() {
        return field1;
    }
...
}

the table in database has the same fields.

I want to select field1 and field2 only. so I tried:

List<A> list = (List<A>) ession.createCriteria(A.class)
                    .setProjection(Projections.projectionList()
                                 .add(Projections.property("field1"))
                                 .add(Projections.property("field1"))
                                 .list();

It is obviously not correct: java.lang.ClassCastException!!

Is there any way to get a class A’s list? i want to use A’s method Directly, like:

list.get(0).getField1()

thks…….

  • 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-22T16:32:09+00:00Added an answer on May 22, 2026 at 4:32 pm

    If you set a projection on the criteria, the query won’t return a list of entities anymore, but a list of arrays of scalars. This is a good thing, since returning a partially loaded entity would result in a multitude of bugs due to invariants being broken (non null fields being null, unloaded associations, etc.).

    So, you should use another kind of object (a value object) to represent the loaded fields :

    List<Object[]> result = criteria.list();
    for (Object[] row : result) {
        AValueObject a = new AValueObject((String) row[0], (String) row[1]);
        // ...
    }
    

    Hibernate can also do this transformation for you, using reflection, using an AliasToBeanResultTransformer or an AliasToBeanConstructorResultTransformer.

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

Sidebar

Related Questions

I am trying to test a dao method which uses the hibernate criteria API,
Using Hibernate 's Criteria API , I want to select the record within a
How would I go about executing the following Hibernate query using the criteria API.
I am retrieving a list of objects in hibernate using Criteria API. However I
I'd like to use Hibernate's Criteria API for precisely what everybody says is probably
I need to replicate the following working HQL query using criteria API. session.CreateQuery( select
does anyone know if you can do something like this using the (N)Hibernate criteria
Is is possible using Hibernate (prefferably the Criteria API) to use a mySql function
The Hibernate Doc state: Finally, note that cascading of operations can be applied to
I'm trying to learn the hibernate criteria API but I'm puzzled by the criteria

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.