Hashmap Cannot Be Cast

java.util.HashMap cannot be cast to Object

java.lang.ClassCastException: java.util.HashMap cannot be cast to custom data class

java.util.HashMap cannot be cast (android studio, firebase,java)

java.util.HashMap cannot be cast to com.google.android.gms.maps.model.LatLng

java.lang.ClassCastException: java.util.HashMap cannot be cast to in spring boot microservice

First, let's create constructors with parameters that you need for RoomInvestigatorMapping and Employee, then you should add these lines:

For RoomInvestigatorMapping

public RoomInvestigatorMapping() {
// Empty constructor
}

public RoomInvestigatorMapping(String sEmpName, Integer nEmpId,
Integer nRoomInvestigatorMappingId,
Integer nRoomAllocationId) {
this.employee = new Employee(sEmpName, nEmpId);
this.nRoomInvestigatorMappingId = nRoomInvestigatorMappingId;
this.nRoomAllocationId = nRoomAllocationId;
}

For Employee

public Employee() {
// Empty constructor
}

public Employee(String sEmpName, Integer nEmpId) {
this.sEmpName = sEmpName;
this.nEmpId = nEmpId;
}

Second, let's modify your query like:

@Query("SELECT new com.spacestudy.model.RoomInvestigatorMapping(emp.sEmpName as sEmpName, emp.nEmpId as nEmpId,"   
+ " roomInvest.nRoomInvestigatorMappingId as nRoomInvestigatorMappingId,"
+ " roomInvest.nRoomAllocationId as nRoomAllocationId) "
+ "FROM RoomInvestigatorMapping as roomInvest "
+ "INNER JOIN Employee as emp "
+ "ON roomInvest.nInvestigatorId = emp.nEmpId ")
List<RoomInvestigatorMapping> findInvestigatorUsingName();

java.util.HashMap cannot be cast to java.util.ArrayList

Hashmap: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

You are getting id field from client with type of Long and trying to put it into an int type.
If you can change Product class, I recommend to change its id field type to long so that no casting is needed also you might face data loss in casting long to int.

by the way if you can not change your code or don't want to, here you can fix the problem:

instead of:

int id = prod.containsKey("id") && !prod.get("id").isEmpty() ? Integer.parseInt((String) prod.get("id")) : 0;

use this:

int id = prod.containsKey("id") && !prod.get("id").isEmpty() ? (int) prod.get("id") : 0;

good luck



Related Topics



Leave a reply



Submit