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.

1
  • @ v.ladynev please help sir. Commented Apr 27, 2016 at 8:06

4 Answers 4

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() {
}
Sign up to request clarification or add additional context in comments.

7 Comments

@ 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
@yashpalbharadwaj You need to add a constructor with two String arguments to the CrbtSubMasterDemo. And you need a default constructor too.
@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)
add space after new, sorry
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)
|
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

2 Comments

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.
@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[]>.
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

6 Comments

this was a real solution by the way. Can you give me where I can Explore my Criteria/Hibernate concepts?
@ sunixi please explain why we are not commiting the transaction here
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();
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 >
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.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.