Spring boot 库方法的投影上的ClassCastException

Spring boot 库方法的投影上的ClassCastException,spring-boot,spring-data-rest,classcastexception,Spring Boot,Spring Data Rest,Classcastexception,java.lang.ClassCastException:au.com.parcelpoint.domain.batch.JobEntity不能强制转换为au.com.parcelpoint.domain.batch.projection.JobProjection 堆栈跟踪指向此行: JobProjection jobProjection jobRepository.findSummaryByIdAndRetailerId(jobId, retailerId); 我试图减少实际持久性对象的

java.lang.ClassCastException:au.com.parcelpoint.domain.batch.JobEntity不能强制转换为au.com.parcelpoint.domain.batch.projection.JobProjection

堆栈跟踪指向此行:

JobProjection jobProjection 
jobRepository.findSummaryByIdAndRetailerId(jobId, retailerId);
我试图减少实际持久性对象的简单投影中所需的字段。我的投影对象称为JobProjection,持久化对象称为JobEntity

这是我的职业预测课:

@Projection(
        name = "jobProjection",
        types = { JobEntity.class })
public interface JobProjection {
    @Value("#{target.id}")
    Long getId();

    @Value("#{target.status}")
    JobEntity.Status getStatus();

    @Value("#{target.batches}")
    List<BatchEntity> getBatches();

    @Value("#{target.createdOn}")
    Date getCreatedOn();

    @Value("#{target.retailerId}")
    Long getRetailerId();
}
投影( name=“jobProjection”, 类型={JobEntity.class}) 公共界面工作计划{ @值(“#{target.id}”) 长getId(); @值(“#{target.status}”) JobEntity.Status getStatus(); @值(“#{target.batches}”) 列出getBatches(); @值(“#{target.createdOn}”) 日期getCreatedOn(); @值(“#{target.retailerId}”) Long getRetailerId(); } 这是我的JobRepository类(压缩):

@RepositoryRestResource(摘录projection=JobProjection.class)
公共接口JobRepository扩展了Crudepository、JobCustom{
JobProjection findSummaryByIdAndRetailerId(长jobId,长RetaileId);
}

为什么您需要在此行中将返回类型设置为JobProjection
JobProjection findSummaryByIdAndRetailerId(Long jobId,Long RetaileId)应该是JobEntity,对吗?您的投影将通过存储库中获取的结果顶部的摘录自动应用。也请看这个例子:-链接中的第5点将返回类型作为投影而不是实际的持久性对象的目的是访问持久性对象的有限形式版本。另一个baeldung教程将findBy“…”方法的返回类型作为投影,而不是实际的hibernate持久化对象。
@RepositoryRestResource(excerptProjection = JobProjection.class)
public interface JobRepository extends CrudRepository<JobEntity, Long>, JobCustom {
        JobProjection findSummaryByIdAndRetailerId(Long jobId, Long retailerId);
}