Featured post
java - can I specify the return type in the hql words -
hi: using hibernate3.6,i meet problem when querying.
take hql exmaple:
string hql="select count(distinct ip),sum(bytes) entity en .... query q=session.createquery(hql); list<?> list=q.list();
now can retrive columen properties list following way:
string[] properties={"count","sum"}; (object obj : list) { map<string, object> m = new hashmap<string, object>(); object[] props = (object[]) obj; if(properties.length!=props.length) throw .... (int = 0; < props.length; i++) { m.put(properties[i], props[i]); } }
now,my problem that,when hql result no set,the count 0,but sum null. want 0 also.
of course,i can converting explicitly if know column name , type(they count,sum in above example) of result table db,but did not know.
that's say,i can not use this:
if(props[i]=null) props[i]=0; m.put(properties[i], props[i]);
since type of props[i] may string,in case if its value null, should "".
so wonder if can sepcify converting type in hql words? maybe someting this:
select count(distinct ip), (long) sum(bytes) entity en ....
select count(distinct ip),(converet ..sum(bytes)) entity en ....
is possible?
bwt,i use mysql 5.1.
thanks.
update: @zinan.yumak:
your answer seems this;
select new wrapper(count(distinct ip),sum(bytes)) entity en where...
if have create enougth extra class wrapper since there many types of select in appliction.like:
select new anotherwrapper(broswer,count(distinct brower)) entity en where... ......
i think need result transformer. create model class query. add null checks or other checks setter methods. this,
string hql="select count(distinct ip) ip, sum(bytes) bytes entity en .... query q=session.createquery(hql).setresulttransformer(transformers.aliastobean(model.class));
and in model class,
class model { private string ip = ""; private bigdecimal bytes = bigdecimal.zero; public void setip(string anip) { if ( astring != null) { this.ip = anip; } } public void setbytes(string abyte) { if ( abyte != null) { this.byte = abyte; } } . . . }
- Get link
- X
- Other Apps
Comments
Post a Comment