0

I have the following code in java.

List<UserHelper> users=List<UserHelper>)session.getNamedQuery("PkUser.loadHelperUsers").list();,

I think it does not matter what the "UserHelper" class is that's why I do not write it, not to overload my question. This is my namedQuery mentioned above.

 @NamedQuery(name = "PkUser.loadHelperUsers", query = "SELECT  new ge.tec.pto.ext.helpers.UserHelper(u) from PkUser u order by u.pkUserId desc"),

The problem is that the hql selects too many rows, I think the same number of rows that is in database in pk_user table.If anyone knows how to fix this please inform me. It will be very nice if the solution will not require to alter my "NamedQuery", It will be graet if I will have to change only my Query creation, But any solutions will be helpful, Thank you

  • what you mean by **too many rows**? Since there is no condition in the query, it will return as many rows as the records in pk_user table. What are you expecting here? – spiritwalker Feb 12 '13 at 11:12

1 Answers1

0
Multiple selects when using Key word “`new`” in `hql`


There is no problem with your code and with NEW keyword .

Your query will return all the Rows in the UserHelper related Table

You should use a WHERE clause to get the required rows .

EX :

query = "SELECT  new ge.tec.pto.ext.helpers.UserHelper(u) from PkUser u where username=:passedparamer order by u.pkUserId desc"
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thank you for replying but I think I could not explain clearly, the bug is that hibernate executes and writes in java console the same select many times, I think the number of select the hibernate executes(the same select) is the number of rows that the select returns.. – Zura Sekhniashvili Feb 12 '13 at 12:42
  • That depends on the lists and mappings of the pojo and if you see those queries those are all different queries .finally it will generate a single ResultSet. – Suresh Atta Feb 12 '13 at 13:01