通过JobExcerpt使用投影来获取实体类JobEntity的特定字段。

JobEntity类:https://pastebin.com/GPeQDiRp

JobExcerpt类:

@Projection(
        name = "jobProjection",
        types = { JobEntity.class })
public interface JobExcerpt {
    Long getId();

    JobEntity.Status getStatus();

    List<BatchEntity> getBatches();

    Date getCreatedOn();

    Long getRetailerId();
}


很简单的JobExcerptRepository类:

@RepositoryRestResource(excerptProjection = JobExcerpt.class)
public interface JobExcerptRepository extends JpaRepository<JobEntity, Long> {

    JobExcerpt findByIdAndRetailerId(Long jobId, Long retailerId);

}


它失败的代码行:

JobExcerpt jobExcerpt = jobExcerptRepository.findByIdAndRetailerId(jobId, retailerId);


问题是...在此代码行中没有类似于运算符的可见(强制转换)。不应该显示它,对不对?

堆栈跟踪:
https://pastebin.com/iNG8h5dn

最佳答案

好像您返回了作业摘要投影。这就是为什么您收到ClassCastException。应该是这样

@RepositoryRestResource(excerptProjection = JobExcerpt.class)
public interface JobExcerptRepository extends JpaRepository<JobEntity, Long> {

    JobEntity findByIdAndRetailerId(Long jobId, Long retailerId);

}

JobEntity jobEntity = jobExcerptRepository.findByIdAndRetailerId(jobId, retailerId);

10-07 12:25