Reputation: 2421
I am trying to fetch the matched string character by using spring data jpa query method and spring boot. When I am implementing this , I am getting the error like the following,
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.spacestudy.model.RoomInvestigatorMapping
My service file containes the following code,
public List<String> retrieveReponsiblePerson(String sEmpName)
{
List<RoomInvestigatorMapping> roomInvestigatorListResultObj = roomInvestigatorMappingRepositoryObj.findInvestigatorUsingName();
List<String> roomInvestigatorListReturnObj = new ArrayList<>();
for (RoomInvestigatorMapping result : roomInvestigatorListResultObj)
{
String subStringRoomInvestigator = result.employee.sEmpName;
if (subStringRoomInvestigator.contains(sEmpName))
{
List<String> obj = new ArrayList<>();
obj.add(subStringRoomInvestigator);
roomInvestigatorListReturnObj.addAll(obj);
}
}
return roomInvestigatorListReturnObj;
}
And I am calling this method from controller file like the following,
@GetMapping("/loadResponsiblePersons")
public List<String> loadResponsiblePersonsMethod(
@RequestParam(value = "sEmpName", required = true) String sEmpName)
{
return roomServiceObj.retrieveReponsiblePerson(sEmpName);
}
And my repository file containes the following,
@Repository
public interface RoomInvestigatorMappingRepository extends JpaRepository<RoomInvestigatorMapping, Integer>{
// find RoomInvestigatorMapping details by id
@Query(" select new map(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();
}
Error stack like the following when I am calling the API,
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.spacestudy.model.RoomInvestigatorMapping
at com.spacestudy.services.RoomService.retrieveReponsiblePerson(RoomService.java:210) ~[classes/:na]
at com.spacestudy.controller.RoomController.loadResponsiblePersonsMethod(RoomController.java:59) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_141]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_141]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_141]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_141]
My RoomInvestigatorMapping.java like the following,
@Entity
@Table(name="roominvestigatormapping")
public class RoomInvestigatorMapping implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "roominvestigatormapping_seq_generator")
@SequenceGenerator(name = "roominvestigatormapping_seq_generator", sequenceName = "roominvestigatormapping_seq",allocationSize=1)
@Column(name="nroom_investigator_mapping_id",columnDefinition="serial")
public Integer nRoomInvestigatorMappingId;
@Column(name="nroom_allocation_id")
public Integer nRoomAllocationId;
@Column(name="ninvestigator_id")
public Integer nInvestigatorId;
@Min(0)
@Max(100)
@Column(name="npercentage_assigned")
public Integer nPercentageAssigned;
@Column(name="scomment")
public String scomment;
@ManyToOne(optional=true)
@JoinColumn(name="ninvestigator_id",referencedColumnName="nemp_id", insertable = false, updatable = false)
public Employee employee;
@ManyToOne(optional=true)
@JoinColumn(name="nroom_allocation_id", insertable = false, updatable = false)
public RoomDepartmentMapping roomDepartmentMapping;
}
What is causing this error?
Upvotes: 1
Views: 1954
Reputation: 5978
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();
Upvotes: 1
Reputation: 3170
In your Repository
interface @Query
return a Map that is issue for class Casting.
you are selecting a new Map and expecting a RoomInvestigatorMapping
entity that cannot done by Hibernate.
select new map(emp.sEmpName as sEmpName, emp.nEmpId as nEmpId, " + "roomInvest.nRoomInvestigatorMappingId as nRoomInvestigatorMappingId, " + "roomInvest.nRoomAllocationId as nRoomAllocationId)
The above query will return List<Map<String, Object>>
.
Upvotes: 0