postgresql - How to get List of Table Entity with only selected columns in Hibernate using native SQL? -
i trying execute sql query using session.createsqlquery() method of hibernate.
test table has 3 columns :
- col1
- col2
- col3
working
string sql = "select * test"; sqlquery query = session.createsqlquery(sql); query.addentity(test.class); list<test> testentitylist = query.list(); not working
string sql = "select col1, col2 test"; sqlquery query = session.createsqlquery(sql); query.addentity(test.class); list<test> testentitylist = query.list(); error:
the column col3 not found in resultset.
i need retrieve a few specific columns table rather whole table.
how can achieve this?
you can use hibernate projections, see answer hibernate criteria query specific columns or can changing return type
list<object[]> , parsing list<test>
list<object[]> testentitylist = query.list(); list<test> res = new arraylist<test>(testentitylist.size()); (object[] obj : testentitylist) { test test = new test(); test.setcol1(obj[0]); test.setcol2(obj[1]); res.add(test); }
Comments
Post a Comment