java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to className

java lang string cannot be cast to (ljava lang object)
java lang classcastexception cannot be cast to
java lang object cannot be cast to java lang string
java lang classcastexception java lang string cannot be cast to
java lang classcastexception java lang stackoverflowerror cannot be cast to java lang exception
java lang string cannot be cast to java util list
java util arraylist cannot be cast to java lang string

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.

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

hibernate java.lang.ClassCastException: [Ljava.lang.Object; cannot , Hibernate Mapping - Object cannot be cast to. Post by: Its: "java.lang.​ClassCastException: [Ljava.lang.Object class name= "database. java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.spring.model.Instruction 0 how to store log files in directory (directory name having current date) every day

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() {
}

Hibernate Mapping, have anyone had problems with casting to table class from iterator? Hibernate <class name="sample. <id name="username" column="username" type="java.​lang.String"/> You cannot delete your posts in this forum  toArray()method is defined in List interface so every where there is an instance of List, you also have access to this method. At first you might think that you can cast an array of Objects which its elements are all of type String to a String array but java specs says otherwise Link, in short it says:

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

java.lang.ClassCastException: [Ljava.lang.Object, class cast exception occurs while type casting the list object which is return from <class name="com. java.lang.ClassCastException: [Ljava.lang.Object; com.​BO.account. SQLGrammarException: could not execute query This Java example demonstrates the usage of ClassCastException class with an example. ClassCastException has thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an

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

class cast exception while casting the list object , Integer cannot be cast to class java.lang.Long queryForList returns an object of Integer and we try to convert it into a Long directly:. java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String when you try to cast an array kind of a class to the linear type. In this case, attempting to cast String -> String[] or vice versa will cause the JVM to throw this particular exception at runtime. Compilers cannot catch this oversight.

java.lang.ClassCastException: class java.lang.Integer cannot be , Java provides a class with name Class in java.lang package. The class name for which Class object is to be created is determined at run-time. provoked by this method fails ClassNotFoundException - if the class cannot be located the object after casting, or null if obj is null Throws : ClassCastException - if the object is  Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException : Constructs a ClassCastException with no detail message. Constructs a ClassCastException with the specified detail message. Constructs a ClassCastException with no

Java.lang.Class class in Java, JDK-8225644 : C1 dumps incorrect class name in ClassCastException message. Type: Bug Method.invoke(Method.java:567) at com.sun.javatest.regtest.agent. Object; cannot be cast to class [Ljava/lang/String; ([Ljava.lang.Object; is in  In my application I need to convert my arraylist to a string of an array. However, I am getting an error: ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] android At th

JDK-8225644 C1 dumps incorrect class name in , Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: [Ljava.​lang.Object; cannot be cast to university.pojo.Schedule - swing. REST with Spring The canonical reference for building a production grade API with Spring.

Comments
  • @ v.ladynev please help sir.
  • 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.
  • @ 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)
  • 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[]>.