【问题标题】:java.lang.Object; cannot be cast to model class: error in Spring Bootjava.lang.对象;无法转换为模型类:Spring Boot 中的错误
【发布时间】:2020-06-05 15:04:36
【问题描述】:

当我使用 Spring Boot 使用 JPQL 查询获取数据库数据并尝试循环数据时,我收到以下错误,

{
"message": "[Ljava.lang.Object; cannot be cast to com.spacestudy.model.RoomCPCMapping",
"error": "Internal Server Error",
"path": "/spacestudy/rockefeller/survey/surveyform/occupant/getClientCPCWithPercentage"
}

我的存储库查询如下,

@Query("SELECT u.nCCPCode,u.nPercent FROM RoomCPCMapping u JOIN u.clientCPC ur where u.nRoomAllocationId=:nRoomAllocationId")
List<RoomCPCMapping> findNCCPCodeByNRoomAllocationID(@Param(value="nRoomAllocationId") Integer nRoomAllocationId );

我正在调用如下查询函数,

List<RoomCPCMapping> 
        roomCpcMappingCodeObj = roomCPCMappingRepositoryObj.findNCCPCodeByNRoomAllocationID(nRoomAllocationID);

通过使用结果对象,我尝试如下循环,

for(RoomCPCMapping rpcLoopObj:roomCpcMappingCodeObj)
    {
        if(clientCpcCodeMappingLoopObj.nClientCPCMappingId==rpcLoopObj.getnCCPCode())                    
             {
                clientCpcCodeMappingLoopObj.nPercentage=rpcLoopObj.nPercent;
                }
    }

我的模型类如下,

@Entity
@Table(name="roomccpcmapping")
public class RoomCPCMapping implements Serializable
{   

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "roomccpcmapping_seq_generator")
@SequenceGenerator(name = "roomccpcmapping_seq_generator", sequenceName = "roomccpcmapping_seq",allocationSize=1)

@Column(name="nroom_ccpc_mapping_id", columnDefinition="serial")
public Integer nRoomCcpcMappingId;

@Column(name="nroom_allocation_id")
public Integer nRoomAllocationId;

@Column(name="nccp_code")
public Integer nCCPCode;

@Column(name="npercent")
public Integer nPercent;

@Column(name="nresponsible_person_id")
public Integer nResponsiblePersonId;

@ManyToOne(optional = true, cascade = { CascadeType.MERGE })
@JoinColumn(name = "nccp_code", insertable = false, updatable = false)
public ClientCostPoolCodes clientCPC ;

public Integer getnRoomCcpcMappingId()
{
    return nRoomCcpcMappingId;
}

public void setnRoomCcpcMappingId(Integer nRoomCcpcMappingId) 
{
    this.nRoomCcpcMappingId = nRoomCcpcMappingId;
}

public Integer getnRoomAllocationId()
{
    return nRoomAllocationId;
}

public void setnRoomAllocationId(Integer nRoomAllocationId)
{
    this.nRoomAllocationId = nRoomAllocationId;
}

public Integer getnCCPCode() 
{
    return nCCPCode;
}

public void setnCCPCode(Integer nCCPCode) 
{
    this.nCCPCode = nCCPCode;
}

public Integer getnPercent()
{
    return nPercent;
}

public void setnPercent(Integer nPercent)
{
    this.nPercent = nPercent;
}

public Integer getnResponsiblePersonId() 
{
    return nResponsiblePersonId;
}

public void setnResponsiblePersonId(Integer nResponsiblePersonId)
{
    this.nResponsiblePersonId = nResponsiblePersonId;
}

public ClientCostPoolCodes getClientCPC() 
{
    return clientCPC;
}

public void setClientCPC(ClientCostPoolCodes clientCPC)
{
    this.clientCPC = clientCPC;
}


public RoomCPCMapping(Integer nRoomCcpcMappingId, Integer nRoomAllocationId, Integer nCCPCode, Integer nPercent,
        Integer nResponsiblePersonId, ClientCostPoolCodes clientCPC) {
    super();
    this.nRoomCcpcMappingId = nRoomCcpcMappingId;
    this.nRoomAllocationId = nRoomAllocationId;
    this.nCCPCode = nCCPCode;
    this.nPercent = nPercent;
    this.nResponsiblePersonId = nResponsiblePersonId;
    this.clientCPC = clientCPC;
}

public RoomCPCMapping() {

 }
}

为什么我会收到这些类型的错误?

【问题讨论】:

  • 服务器应该记录堆栈跟踪并且应该有一行发生了这个异常。提供堆栈跟踪和发生错误的行或方​​法/类的块。 Java 是静态类型语言,通常它可以防止编译级别的类型错误。我猜你的代码违反了一些通用和静态类型规则。

标签: java spring-boot jpa spring-data-jpa


【解决方案1】:

选项 1) 确保 RoomCPCMapping 是投影接口:

public interface RoomCPCMappingResult {

    String getNCCPCode();
    String getNPercent();

    ...
}

选项 2) 使用结果类遗留选项:

SELECT new com.my.package.RoomCPCMappingResult(u.nCCPCode,u.nPercent)
FROM RoomCPCMapping u JOIN u.clientCPC ur 
where u.nRoomAllocationId=:nRoomAllocationId

只要确保那里有足够的构造函数即可。

【讨论】:

  • 感谢您的回复。我将使用第二个选项。在这里,我已经有了带参数的构造函数。如何添加足够的构造函数?
  • 它必须是一个只有这两个参数并且顺序适当的一个
  • 另外忘记添加选项 2 也需要一个单独的类。例如 RoomCPCMappingResult。
【解决方案2】:

如 Maciej Kowalski 所述,您可以使用带有两个参数构造函数的单独 bean。

@Query("SELECT u.nCCPCode,u.nPercent FROM RoomCPCMapping u JOIN u.clientCPC ur where u.nRoomAllocationId=:nRoomAllocationId")
List<RoomCPCMapping> findNCCPCodeByNRoomAllocationID(@Param(value="nRoomAllocationId") Integer nRoomAllocationId );

或者 ELSE 只需更新查询

   @Query("SELECT u FROM RoomCPCMapping u JOIN u.clientCPC ur where u.nRoomAllocationId=:nRoomAllocationId")
    List<RoomCPCMapping> findNCCPCodeByNRoomAllocationID(@Param(value="nRoomAllocationId") Integer nRoomAllocationId );

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2022-08-14
    • 2020-08-06
    • 2021-09-07
    • 1970-01-01
    • 2018-03-22
    • 2023-04-03
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多