java.lang.Object; cannot be cast to model class: error in Spring Boot

Jacob :

When I am fetching database data using JPQL query using spring boot and trying to loop the data , I am getting the following error,

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

My repository query like the following ,

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

And I am calling the query function like the following,

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

By using the result object , I am trying to loop like the following,

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

My Model class like the following,

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

 }
}

Why I am getting these type of errors?

Maciej Kowalski :

Option 1) Make sure the RoomCPCMapping is a projection interface:

public interface RoomCPCMappingResult {

    String getNCCPCode();
    String getNPercent();

    ...
}

Option 2) Use the result class legacy option:

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

just make sure you have an adequate constructor present there.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19914&siteId=1