java - Hibernate - Use native query and alias to Bean with enum properties? -
i having trouble using native query in hibernate alias bean contains enum properties. getting invocationtargetexception when query.list() called. example 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 prop1, col2 prop2, col3 prop3 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 query.addscalar("col3", hibernate.string) call, don't know type use col3 (the enum type). hibernate.string not working! have tried leave type off entirely ( query.addscalar("col3") ) same invocationtargetexception. can me out this? model cannot changed , stuck native sql query. ideas appreciated.
firstly, shouldn't use
private enumtype prop3;
but
private actualenum prop3;
where actualenum
own enum type (for example, fruits
distinguish apples , oranges).
second, hibernate mapping irrelevant when use native sql.
now, there couple of options can propose. can try use addentity()
instead of bunch of scalars. it's possible hibernate recognize enum property , map correctly.
other option have non public setter take string database, convert enum , set actual property.
finally, can customize transformer. it's complex option.
Comments
Post a Comment