Java.Lang.Classcastexception: [Ljava.Lang.Object; Cannot Be Cast to Classname

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

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

Special Scenario : java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

This worked for me finally

values.forEach(o->{ strings.add(Arrays.copyOf(o, o.length, String[].class));});

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.infotech.model.Transaction(class)

The problem here is that when you use projections Hibernate returns List<Object[]> (not List<Transaction>).

The simplest thing is to change the HQL query, just to check that everything works

Query query1 = session.createQuery("from Transaction t where t.user_id=:uid");

Please, read this if you want to use projections

SpringBoot+Hibernate+Restful : format response

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

Because you are using a multi-selection projection, you are actually fetching an array of objects, so you need to change the query result processing logic to:

List<Object[]> tuples = (List<Object[]>) session.createQuery(query).list();

for(Object[] tuple : tuples) {
UserEntity ue = tuple[0];
Number roleId = tuple[1];
}

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to iland.hbm.BillDetails`

First of all this query can't work as it is. I already responded about the need for including all fetched entities' properties in the group by clause.

You try to select both an entity and a sum, while expecting to fit into a List<BillDetails>.

If you were only selecting BillDetails you'd be fine. But when you want to select more than one entity you need to assign the select to a List<Object[]>:

List<Object[]> obj = null;
...
Object[] bdAndSum = obj.get(0);
BillDetails bd = (BillDetails) bdAndSum.get(0);
Number sum = (Number) bdAndSum.get(1);

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to my custom model class

I got this Now.I stored the result as an object array and Iterated over it.I then explicitly casted each element to my pojo class elements.The problem i was facing earlier was the first parameter the query result was big integer and my pojo had Int field. Thanks @rkosegi .



Related Topics



Leave a reply



Submit