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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:54:46+00:00 2026-06-15T05:54:46+00:00

I am using a SQL query and then transforming the result using Hibernates’s Transformers.aliasToBean()

  • 0

I am using a SQL query and then transforming the result using Hibernates’s Transformers.aliasToBean().
One of the columns in my query is an enum. The transformation somehow fails for the enum. What should I do? Which datatype should I use? I want more than 1 character to transform the result into my enum type.

This is how the simplified version of my query/code looks like (b is an enum in the table profiles):

session.createSQLQuery("select a, b from profiles").setResultTransformer(Transformers.aliasToBean(Profile.class))
                    .list();

Exception : expected type: Foo.ProfileStateEnum, actual value: java.lang.Character

  • 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-15T05:54:48+00:00Added an answer on June 15, 2026 at 5:54 am

    Assuming that the java enum type that corresponds to column b is Foo.ProfileStateEnum, the following code snippet should work for you. (I tested with Hibernate 4.1.6)

    import java.util.Properties;
    import org.hibernate.type.Type;
    import org.hibernate.type.IntegerType;
    import org.hibernate.internal.TypeLocatorImpl.TypeLocatorImpl;
    import org.hibernate.type.TypeResolver.TypeResolver;
    import org.hibernate.type.EnumType;
    
    Properties params = new Properties();
    params.put("enumClass", "Foo.ProfileStateEnum");
    params.put("type", "12"); /*type 12 instructs to use the String representation of enum value*/
    /*If you are using Hibernate 5.x then try:
    params.put("useNamed", true);*/
    Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(EnumType.class, params);
    
    List<Profile> profileList= getSession().createSQLQuery("select a as ID, b from profiles")
                .addScalar("ID", IntegerType.INSTANCE)
                .addScalar("b", myEnumType )
                .setResultTransformer(Transformers.aliasToBean(Profile.class))
                .list();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this issue: first, I execute a SQL query using Java and then
I am calling one function using dynamic SQL query which accepts TVP. Is there
I'm using SQL Query Analyzer to build a report from the database on one
I'm using an sql query to search my table of posts for hashtags then
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOID
Snippet from a SQL query (using SQL Server 2008 R2): , CONVERT (VARCHAR(10), s.ENTER_DATE,
I am trying to learn SQL query optimization using SQL Server 2005. However, I
How to change column order in a table using SQL query in SQL Server

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.