gpt4 book ai didi

java.lang.Object;无法转换为模型类 : error in Spring Boot

转载 作者:行者123 更新时间:2023-12-01 10:34:00 25 4
gpt4 key购买 nike

当我使用 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() {

}
}

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

最佳答案

选项 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

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

关于java.lang.Object;无法转换为模型类 : error in Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60334327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com