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
Java Regex to Validate Full Name Allow Only Spaces and Letters
How to Test Code Dependent on Environment Variables Using Junit
Better Way to Detect If a String Contains Multiple Words
Retrieving Data from Biometric Fingerprint Attendance Device
How to Write a User Input Based Constructor
Update Objects in One List Based on Values from Second One Using Streams
Java - How to Find Students With Their Highest Marks Writing a Method in a Student Class
Convert Multi If-Else to Simple Lambda Expression Code in Java
Convert Yyyy-Mm-Dd Hh:Mm:Ss.Sss Zzz Format Strin to Date Object
How to Store User Input into an Existing Array
How to Handle JPA Unique Constraint Violations
How to Return a HTML Page from a Restful Controller in Spring Boot
Regular Expression for Excluding Special Characters
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate
Passing List from Thymeleaf to Spring Controller
Get Current Week Start and End Date in Java - (Monday to Sunday)
How to Import Two Classes With the Same Name in Different Packages