Jac*_*cob 8 java jpa spring-data-jpa spring-boot
当我使用 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"
}
Run Code Online (Sandbox Code Playgroud)
我的存储库查询如下所示,
@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 );
Run Code Online (Sandbox Code Playgroud)
我正在调用如下所示的查询函数,
List<RoomCPCMapping> 
        roomCpcMappingCodeObj = roomCPCMappingRepositoryObj.findNCCPCodeByNRoomAllocationID(nRoomAllocationID);
Run Code Online (Sandbox Code Playgroud)
通过使用结果对象,我试图像下面这样循环,
for(RoomCPCMapping rpcLoopObj:roomCpcMappingCodeObj)
    {
        if(clientCpcCodeMappingLoopObj.nClientCPCMappingId==rpcLoopObj.getnCCPCode())                    
             {
                clientCpcCodeMappingLoopObj.nPercentage=rpcLoopObj.nPercent;
                }
    }
Run Code Online (Sandbox Code Playgroud)
我的模型类如下所示,
@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() {
 }
}
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这些类型的错误?
选项1)确保RoomCPCMapping是投影接口:
public interface RoomCPCMappingResult {
    String getNCCPCode();
    String getNPercent();
    ...
}
Run Code Online (Sandbox Code Playgroud)
选项 2)使用结果类遗留选项:
SELECT new com.my.package.RoomCPCMappingResult(u.nCCPCode,u.nPercent)
FROM RoomCPCMapping u JOIN u.clientCPC ur 
where u.nRoomAllocationId=:nRoomAllocationId
Run Code Online (Sandbox Code Playgroud)
只要确保那里有足够的构造函数即可。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1265 次  |  
        
|   最近记录:  |