8

I have:

@Column(name = "UserType", nullable = false)
@Enumerated(EnumType.STRING)
private UserType userType;

How to return this field by SQL request using addScalar() method?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Dark Hydra
  • 265
  • 6
  • 21

1 Answers1

10

This is how you add an Enum to a SQLQuery

  1. You define a properties object

    Properties params = new Properties();
    params.put("enumClass", "UserType");
    params.put("type", "12"); /*EnumType.STRING type = 12 */
    
  2. You resolve your enum type:

    Type userEnumType = sessionFactory.getTypeHelper().custom(UserType.class, params);
    
  3. You execute your query:

    List<SomeEntity> result = getSession().createSQLQuery("select e from SomeEntity ")
        .addScalar("userType", userEnumType)
        .setResultTransformer(Transformers.aliasToBean(SomeEntity.class))
        .list();
    
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I got error org.hibernate.MappingException: Unable to instantiate custom type: UserType. Also some people put in custom method EnumType.class. Then I got same error: org.hibernate.MappingException: Unable to instantiate custom type: org.hibernate.type.EnumType. – Dark Hydra Jul 07 '14 at 21:16
  • 1
    UserType is also a Hibernate class. Are you sure you added a class named UserType. – Vlad Mihalcea Jul 07 '14 at 21:20
  • Actually, I wrote UserType only for example. But I use full name to that class like: com.xxx.User.UserType, when put it to params. – Dark Hydra Jul 07 '14 at 21:23
  • I solved this, when move away UserType enumeration from User class. UserType must have its own java file, then all will be good. So I use next enumClass value: com.xxx.UserType. And also I put EnumType.class into custom method. – Dark Hydra Jul 08 '14 at 10:32
  • Does this work with hibernate 5? I used for prior hibernates but now the same code is failing? see http://stackoverflow.com/questions/38039481/enum-property-fails-in-sql-transformation-for-hibernate-5 – bavon Jul 12 '16 at 13:11
  • It should since `addScalar` method is still found in `SQLQuery`. – Vlad Mihalcea Jul 12 '16 at 13:12
  • I actually asked a question on it see this http://stackoverflow.com/questions/38039481/enum-property-fails-in-sql-transformation-for-hibernate-5. Help will be appreciated – bavon Jul 12 '16 at 13:14
  • This worked for me for non-Enum types too, although I didn't need the Properties param. Thanks! – Eric Aug 26 '16 at 16:36
  • @bavon: use `sessionFactory.getTypeHelper().custom` instead of `new TypeLocatorImpl(new TypeResolver()).custom` – izogfif Oct 31 '18 at 12:32
  • Everyone across the web just keeps copying and pasting `/*EnumType.STRING type = 12 */` but what's behind this? – no id Apr 11 '19 at 04:46
  • It's an explanation, telling you might the magic number 12 means in this context. I suppose devs might wonder about it. – Vlad Mihalcea Apr 11 '19 at 05:09
  • how to do for non-enum, how to do for normal class/pojo ? – user1735921 Sep 25 '19 at 00:18