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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:47+00:00 2026-05-23T23:39:47+00:00

I am having trouble using a native query in hibernate to alias a bean

  • 0

I am having trouble using a native query in hibernate to alias a bean that contains enum properties. I am getting an InvocationTargetException when query.list() is called. My example is below:

@Entity(name = "table1")
public class Class1 {
    @Column(name = "col1")
    @NotNull
    private Integer prop1;

    @Column(name = "col2")
    @NotNull
    private String prop2;

    @Column(name = "col3", length = 6)
    @Enumerated(value = EnumType.STRING)
    private MyEnumType prop3;

    // ... Getters/Setters...
}

public List getClass1List(){
    String sql = "select col1 as prop1, col2 as prop2, col3 as prop3 from table1";

    Session session = getSession(Boolean.FALSE);
    SQLQuery query = session.createSQLQuery(sql);
    query.addScalar("col1", Hibernate.INTEGER);
    query.addScalar("col2", Hibernate.STRING);
    query.addScalar("col3", Hibernate.STRING);

    query.setResultTransformer(Transformers.aliasToBean(Class1.class));

    return query.list();
}

During the query.addScalar(“col3”, Hibernate.STRING) call, I don’t know what type to use for col3 (the enum type). Hibernate.String is not working! I have also tried to leave the type off entirely ( query.addScalar(“col3”) ) but I get the same InvocationTargetException. Can anyone help me out with this? My model cannot be changed and I am stuck with a native sql query. Any ideas are 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-05-23T23:39:49+00:00Added an answer on May 23, 2026 at 11:39 pm

    Firstly, you shouldn’t use

    private EnumType prop3;
    

    but

    private ActualEnum prop3;
    

    Where ActualEnum is your own enum type (for example, Fruits to distinguish apples and oranges).

    Second, you hibernate mapping is irrelevant when you use native sql.

    Now, there are couple of options I can propose. You can try to use addEntity() instead of bunch of scalars. It’s possible that Hibernate will recognize enum property and map correctly.

    Other option is to have non public setter that would take string from database, convert it to enum and set actual property.

    Finally, you can customize transformer. But it’s probably most complex option.

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

Sidebar

Related Questions

I am using mssql and am having trouble using a subquery. The real query
I'm using Intellij Idea 9.0.4 on a Mac. I'm having trouble getting the Git
I'm having trouble using an argument from my class in one of that class's
I'm having trouble using properties in a WCF service. I can define a property
I'm having trouble using Rhino Mocks to assert that a method was called (and
LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm
Im having trouble using a .NET COM in vb6, It compiles ok and I
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a

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.