Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
@ v.ladynev please help sir. – yashpal bharadwaj Apr 27 '16 at 8:06
up vote 0 down vote accepted

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

share|improve this answer
    
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<CrbtSubMasterDemo> 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

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() {
}
share|improve this answer
    
@ 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 8: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 8: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 8:27
    
add space after new, sorry – Joe Taras Apr 27 '16 at 8:29
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 8:36
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

share|improve this answer
    
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 8:20
    
@yashpalbharadwaj Don't clear understand what you ask about. For a query from CrbtSubMasterDemo you will have a List<CrbtSubMasterDemo>. For a query select mobile, password FROM CrbtSubMasterDemo you will have a List<Object[]>. – v.ladynev Apr 27 '16 at 8:24

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.