Search code examples
sqlhibernateenumshqltransformation

Hibernate SQL transformation fails for Enum field type


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


Solution

  • 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();