1

There are similar questions here but none of them worked in my case. I have a custom SQL which returns 2 columns: one string, one number. And string column is always a full uppercase ENUM name. I want to feed this result set into my custom bean which has the said enum.

Following the answer here for Hibernate 5.X, code is below

 Properties params = new Properties();
 params.put("enumClass", "MyEnumClass");
 params.put("useNamed", true);
 Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(MyEnumClass.class, params);
 final Query query = getCurrentSession().createSQLQuery(MY_CUSTOM_SQL)
 .addScalar("col1", myEnumType)
 .addScalar("col2", StandardBasicTypes.INTEGER)
 .setLong("someSqlVar", someVal)
 .setResultTransformer(Transformers.aliasToBean(MyCustomBean.class));
 return query.list();

This code does not even execute query.list() method, it fails at this line:

Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(MyEnumClass.class, params);

Exception trace:

Caused by: org.hibernate.MappingException: Unable to instantiate custom type: com.example.MyEnumClass
...
Caused by: java.lang.InstantiationException: com.example.MyEnumClass
...
Caused by: java.lang.NoSuchMethodException: com.example.MyEnumClass.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[?:1.8.0_60]
at java.lang.Class.newInstance(Class.java:412) ~[?:1.8.0_60]
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:202) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:193) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.internal.TypeLocatorImpl.custom(TypeLocatorImpl.java:144) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
...

So hibernate is trying to call MyEnumClass.class.newInstance() and failing. It does not even check for properties I passed. Using Hibernate 5.1.0.Final, am I not supposed to use custom type this way?

asked May 2, 2017 at 22:13

1 Answer 1

3

I found a way to do it:

Properties params = new Properties();
params.put("enumClass", MyEnumClass.class.getName());
params.put("useNamed", true);
EnumType enumType = new EnumType();
enumType.setParameterValues(params);
CustomType customType = new CustomType(enumType);
final Query query = getCurrentSession().createSQLQuery(MY_CUSTOM_SQL)
 .addScalar("col1", customType)
 .addScalar("col2", StandardBasicTypes.INTEGER)
 .setLong("someSqlVar", someVal)
 .setResultTransformer(Transformers.aliasToBean(MyCustomBean.class));
return query.list();
answered May 3, 2017 at 16:57
Sign up to request clarification or add additional context in comments.

1 Comment

After a white night, found this! worked like a charm for Hibernate 5.1.0 ! Thanks a ton!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.