繁体   English   中英

java.lang.ClassCastException:无法在 Spring Boot 微服务中强制转换为 java.util.HashMap

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

我试图通过使用 spring 数据 jpa 查询方法和 spring 启动来获取匹配的字符串字符。 当我实施这个时,我收到如下错误,

java.lang.ClassCastException: java.util.HashMap cannot be cast to com.spacestudy.model.RoomInvestigatorMapping

我的服务文件包含以下代码,

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;
}

我从控制器文件中调用这个方法,如下所示,

@GetMapping("/loadResponsiblePersons")
public List<String> loadResponsiblePersonsMethod(
        @RequestParam(value = "sEmpName", required = true) String sEmpName) 
{
    return roomServiceObj.retrieveReponsiblePerson(sEmpName);
}

我的存储库文件包含以下内容,

@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();
}

当我调用 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]

我的 RoomInvestigatorMapping.java 如下所示,

@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; 

}

是什么导致了这个错误?

首先,让我们使用RoomInvestigatorMappingEmployee所需的parameters创建构造函数,然后您应该添加以下RoomInvestigatorMapping行:

对于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;                    
}

对于员工

public Employee() {
     // Empty constructor
}

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

其次,让我们修改您的查询,如:

@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();

在您的Repository接口中, @Query返回一个 Map,它是类 Casting 的问题。

您正在选择一个新的 Map 并期待一个无法通过 Hibernate 完成的RoomInvestigatorMapping实体。

select new map(emp.sEmpName as sEmpName, emp.nEmpId as nEmpId, " + "roomInvest.nRoomInvestigatorMappingId as nRoomInvestigatorMappingId, " + "roomInvest.nRoomAllocationId as nRoomAllocationId)

上面的查询将返回List<Map<String, Object>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM