See why you get this exception.
From the question, it is obvious that you used the @Enumerated(EnumType.STRING) annotation @Enumerated(EnumType.STRING) for the 'b' field in your model class. Thus, the field is an enumeration for your model class and varchar for your database . Native SQL does not interest you in the model class and returns what it ever was in the database table, as it is. Thus, in your case, the SQLQuery you are using will return String for 'b' instead of the type ProfileStateEnum . But your setter method for 'b' in the Profile class takes an argument of type ProfileStateEnum .
So you get the exception "expected type: Foo.ProfileStateEnum, actual value: java.lang.Character"
You can use the Aliasing function to solve the problem.
What I am suggesting is a column alias with whatever name you want, and create a setter method for that alias in your / dto model.
For example, allows an alias of your column as "enumStr".
Then your request will look like this: " select a, b as enumStr from profiles "
Now create the setter method for this alias in your Profile class.
(Assuming the ProfileStateEnum can have either of the two values STATE1 and STATE2 )
public void setEnumStr(String str){ if(str.equals(ProfileStateEnum.STATE1.toString())){ b = ProfileStateEnum.STATE1; } else { b = ProfileStateEnum.STATE2; } }
Now, when converting, the installer for the alias setEnumStr(String) will be called instead of setter for the setB(ProfileStateEnum) field, and the string will be converted and saved to the type you need without any exceptions.
I'm new to Hibernate and the solution worked for me. I am using PostgreSQL. But I believe that it works for other databases as well.