1

Code:

public void getDetails() {
try {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
String hql = "select c.mobile, c.password FROM CrbtSubMasterDemo c where rownum<20";
Query query = session.createQuery(hql);
List<CrbtSubMasterDemo> itr = query.list();
session.getTransaction().commit();
for (CrbtSubMasterDemo pojo : itr) {//excepion line
System.out.println("[" + pojo.getMobile() + "]");
}
} catch (Exception e) {
e.printStackTrace();
}
}

CrbtSubMasterDemo is pojo mapped with the db. When I try to run it, it gives following Exception:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.telemune.demoPojo.CrbtSubMasterDemo
at com.telemune.demoHibernate.QueryTester.getDetails(QueryTester.java:57)
at com.telemune.demoHibernate.QueryTester.main(QueryTester.java:23)

The question is query.list() is returning the list of objects of pojo class. Then why is this Exception. I am new to Hibernate, sorry if its a silly question.

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
yashpal bharadwaj
  • 323
  • 2
  • 6
  • 14

4 Answers4

3

When you write this:

String hql = "select c.mobile, c.password FROM CrbtSubMasterDemo c where rownum<20";

Your result set is not a List of CrbtSubMasterDemo

Try to write:

String hql = "select FROM CrbtSubMasterDemo c where rownum<20";

Another way is define a new constructor of CrbtSubMasterDemo where you pass only two fields c.mobile, c.password

so your query becomes:

String hql = "select new " + CrbtSubMasterDemo.class.getName() + "(c.mobile, c.password) FROM CrbtSubMasterDemo c where rownum<20";

If you follow this solution, remeber to add a default constructor too (without parameters), so in your pojo you have:

public CrbtSubMasterDemo(String mobile, String password) {
    this.mobile = mobile;
    this.password = password
}

and

public CrbtSubMasterDemo() {
}
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • @ JoeTaras may be I am using it wrong but it turned into: ERROR: Unable to locate appropriate constructor on class [com.telemune.demoPojo.CrbtSubMasterDemo]. Expected arguments are: java.lang.String, java.lang.String Unable to locate appropriate constructor on class Expected arguments are: java.lang.String, java.lang.String – yashpal bharadwaj Apr 27 '16 at 08:25
  • 2
    @yashpalbharadwaj You need to add a constructor with two `String` arguments to the `CrbtSubMasterDemo`. And you need a default constructor too. – v.ladynev Apr 27 '16 at 08:27
  • @yashpalbharadwaj: I've updated my answer add a complete path of class. Another reaso can be if one of two fields are null. Yes, as has written v.ladynew, you must add a default constructor too (without parameters) – Joe Taras Apr 27 '16 at 08:27
  • add space after new, sorry – Joe Taras Apr 27 '16 at 08:29
  • @v.ladynev means I will need an exactly matching parameterized constructor for every different query? – yashpal bharadwaj Apr 27 '16 at 08:34
  • 1
    If you want a variable number of fields yes, otherwise you can use the first solution (where you'll get all POJO rows, more expensely but is automapped) – Joe Taras Apr 27 '16 at 08:36
  • @yashpalbharadwaj It is not very convenient, of course. You can try to use a transformer http://in.relation.to/2006/03/17/hibernate-32-transformers-for-hql-and-sql/. You can transform directly to `CrbtSubMasterDemo` without DTO. – v.ladynev Apr 27 '16 at 08:42
3
String hql = "select c.mobile, c.password FROM CrbtSubMasterDemo c where rownum<20";
Query query = session.createQuery(hql);

Result of this will be List<Object[]>

List<Object[]> itr = query.list();

for (Object[] row : itr) {
  System.out.println(String.format("mobile:%s, password:%s", row[0], row[1]));
}

if mobile and password are strings, of course. You can use a transformer to transform results directly to CrbtSubMasterDemo.

Hibernate 3.2: Transformers for HQL and SQL

FluentHibernateResultTransformer

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • yeah it worked...but can you please tell me.. what's the role of POJO it left with...I didn't use any getter of the class not even the object of pojo . How will I have the Idea that it is working on my POJO or not? thanks for help. – yashpal bharadwaj Apr 27 '16 at 08:20
  • @yashpalbharadwaj Don't clear understand what you ask about. For a query `from CrbtSubMasterDemo` you will have a `List`. For a query `select mobile, password FROM CrbtSubMasterDemo` you will have a `List`. – v.ladynev Apr 27 '16 at 08:24
1

Do not directly cast the result of "query.list();" to List of CrbtSubMasterDemo. As query.list() return object list. Iterate over the object list received and cast one by one to put in list List of CrbtSubMasterDemo

Rahul
  • 309
  • 1
  • 11
1

Sir, Many times user faces this kinda requirements . Hibernate has ResultTransformer to convert a hql/sql in Object.

    public CrbtSubMasterDemo{
         private Stirng mobile;
         private String password;

          public CrbtSubMasterDemo(){
            --------------
         }

        #####after setting the transation set whichever columns you are selecting should be given as name of property of your object
        String hql = "select c.mobile as mobile, c.password as password FROM CrbtSubMasterDemo c where rownum<20";
        Query query = session.createQuery(hql);
        List<CrbtSubMasterDemo> itr = query.setResultTransformer(Transformers.aliasToBean(CrbtSubMasterDemo.class) ).list();
        ##No need to commit the transaction.
    }

It will convert you query into the CrbtSubMasterDemo

sunixi
  • 82
  • 8
  • this was a real solution by the way. Can you give me where I can Explore my Criteria/Hibernate concepts? – yashpal bharadwaj Apr 29 '16 at 10:31
  • @ sunixi please explain why we are not commiting the transaction here – yashpal bharadwaj Apr 29 '16 at 10:34
  • You can use Criteria where you want restrictions. Like Criteria cr = session.createCriteria(CrbtSubMasterDemo.class); cr.add(Restrictions.lt("rownum", 20)); List list=cr.list(); – sunixi Apr 29 '16 at 14:30
  • and yashpal ji criteria will return all coulumns of your table . If you want only selected columns shud be returned then you need to use Projections > – sunixi Apr 29 '16 at 14:36
  • well..I hope we could have a chat box. but sunixi JI I asked 1. why we are not comming the transaction. 2.Can you give me where I can Explore my Criteria/Hibernate concepts. :) in refrence to ##No need to commit the transaction. – yashpal bharadwaj Apr 29 '16 at 14:40
  • Sir , you are doing select operation and commit means you have done some changes in database and after that you want to make it peramanent. and if you want revert it then use rollback. – sunixi Apr 29 '16 at 14:44