Note that there are some explanatory texts on larger screens.

plurals
  1. POcan I specify the return type in the hql words
    primarykey
    data
    text
    <p>Hi: I am using hibernate3.6,I meet some problem when do some querying.</p> <p>Take this hql for exmaple:</p> <pre><code>String hql="select count(distinct ip),sum(bytes) from Entity en where .... Query q=Session.createQuery(hql); List&lt;?&gt; list=q.list(); </code></pre> <p>Now I can retrive the columen properties from the list by the following way:</p> <pre><code> String[] properties={"count","sum"}; for (Object obj : list) { Map&lt;String, Object&gt; m = new HashMap&lt;String, Object&gt;(); Object[] props = (Object[]) obj; if(properties.length!=props.length) throw .... for (int i = 0; i &lt; props.length; i++) { m.put(properties[i], props[i]); } } </code></pre> <p>Now,my problem is that,when the hql result no set,the count is 0,but the sum is null. I want it 0 also.</p> <p>Of course,I can do the converting explicitly if the know the column name and type(they are the count,sum in the above example) of the result table from db,but now I did not know.</p> <p>That's to say,I can not use something like this:</p> <pre><code>if(props[i]=null) props[i]=0; m.put(properties[i], props[i]); </code></pre> <p>Since the type of <strong>props[i]</strong> may be string,in this case if <strong>its value is null</strong>, it should be <strong>""</strong>.</p> <p>So I wonder if I can <strong>sepcify the converting type in the hql words</strong>? maybe someting like this:</p> <blockquote> <p>select count(distinct ip), <strong>(long)</strong> sum(bytes) from Entity en where .... </p> <p>select count(distinct ip),<strong>(converet</strong> ..sum(bytes)) from Entity en where ....</p> </blockquote> <p>Is this possible?</p> <p>BWT,I use mysql 5.1.</p> <p>Thanks.</p> <p><strong>UPDATE:</strong> @zinan.yumak:</p> <p>Your answer seems like this;</p> <pre><code>select new Wrapper(count(distinct ip),sum(bytes)) from Entity en where... </code></pre> <p>If so I have to create enougth <strong>extra class like Wrapper</strong> since there are so many types of select in my appliction.like:</p> <pre><code>select new AnotherWrapper(broswer,count(distinct brower)) from Entity en where... ...... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload