7

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
  • If you are aliasing "col1 as prop1" in your sql statement, will Hibernate be able to map it? Commented Jul 21, 2011 at 21:31

2 Answers 2

3
// In public List getClass1List() method:
// 1. DEFINE:
Properties params = new Properties();
params.put("enumClass", "enumerators.MyEnumType");
params.put("type", "12");

// 2. REPLACE: 
// query.addScalar("col3", Hibernate.STRING);
// X

query.addScalar("col3", Hibernate.custom(org.hibernate.type.EnumType.class, params));
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

I am actually using my own enum type, it was by pure coincidence and carelessness that I used EnumType. Question has been edited, nice catch. I went with the second option before posting, so I will give the first option a try. Feel free to edit your answer to reflect the changes in my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.