hibernate convert into class

I can use origin sql in hibernate, but it will return List<Object[]> . What should I do to let it return my class object? like List<User>?

If class is not a mapping entity:

public List<VStationModel> getStationByCondition(Map qo, int page, int pagecnt){
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(“SELECT * FROM (SELECT PAGETABLE.*, ROWNUM RN FROM (“);
sBuilder.append(“select stationid,code,name,staorsec,BIG_NAME as bigname,mmscode,”);
sBuilder.append(“LIANSUO_TYPE as liansuotype,JIANCE_TYPE as jiancetype,CTCTDCS_TYPE as ctctdcstype,”);
sBuilder.append(“QUJIANBISAI_TYPE as qujianbisai,MMS_VERSION as mmsversion,WXU_TYPE as wxtype,”);
sBuilder.append(“linecodes,linenames,orgnames,orgnums,seq from SYS_V_STA where 1=1 “);
sBuilder.append(buildQuerySql(qo));
sBuilder.append(” order by linenames,orgnames,seq “);
int start = (page – 1) * pagecnt;
int end = page * pagecnt;
sBuilder.append(“)PAGETABLE WHERE ROWNUM<=” + end + “)WHERE RN>” + start);
Session session = this.getHandler().getSessions(); //HibernateDaoSupport.getSession()
SQLQuery query = session.createSQLQuery(sBuilder.toString());
query.addScalar(“stationid”);
query.addScalar(“code”);
query.addScalar(“name”);
query.addScalar(“staorsec”);
query.addScalar(“bigname”);
query.addScalar(“mmscode”);
query.addScalar(“liansuotype”);
query.addScalar(“jiancetype”);
query.addScalar(“ctctdcstype”);
query.addScalar(“qujianbisai”);
query.addScalar(“mmsversion”);
query.addScalar(“wxtype”);
query.addScalar(“linecodes”);
query.addScalar(“orgnames”);
query.addScalar(“orgnums”);
query.addScalar(“seq”,IntegerType.INSTANCE);
query.setResultTransformer(Transformers.aliasToBean(VStationModel.class));
List<VStationModel> result = query.list();
return result;
}

All variables should defined in class VStationModel. Hibernate will try to convert Object[] array into  VStationModel by call the setter function. So, all variables must have public setters.

 

If the clas is an entity

1. use hql;

2. use SQLQuery.addEntity();

 

//reference:

http://www.tuicool.com/articles/Zn2qQf

http://www.cnblogs.com/xiaoluo501395377/p/3378355.html

This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s