Java 类投影中的Spring数据类CastException
我正在尝试使用Spring数据进行类投影。它应该能够正常工作,因为要求是“其构造函数的参数名必须与根实体类的属性匹配。”并且它匹配。但无论出于何种原因,它都会抛出一个异常:Java 类投影中的Spring数据类CastException,java,spring-boot,spring-data-jpa,Java,Spring Boot,Spring Data Jpa,我正在尝试使用Spring数据进行类投影。它应该能够正常工作,因为要求是“其构造函数的参数名必须与根实体类的属性匹配。”并且它匹配。但无论出于何种原因,它都会抛出一个异常: java.lang.ClassCastException: class ws.xx.posts.post.PostSnapshot cannot be cast to class ws.xx.posts.post.dto.query.PostDetailsQueryDTO (ws.xx.posts.post.PostSnap
java.lang.ClassCastException: class ws.xx.posts.post.PostSnapshot cannot be cast to class ws.xx.posts.post.dto.query.PostDetailsQueryDTO (ws.xx.posts.post.PostSnapshot and ws.xx.posts.post.dto.query.PostDetailsQueryDTO are in unnamed module of loader 'app')
值得注意的是,实体位于“posts域”模块中,DTO位于“posts应用程序”模块中。
我已经尝试过在没有Lombok的情况下手动执行,但结果是一样的,因为它会引发异常
实体(在单独的文件orm.xml中配置,这是其值对象):
DTO:
查询报告:
interface SqlPostQueryRepository extends PostQueryRepository, Repository<PostSnapshot, Long> {
@Override
PostDetailsQueryDTO findByPostId(UUID postId);
@Override
Page<PostDetailsQueryDTO> findAll(Pageable pageable);
}
接口SqlPostQueryRepository扩展了PostQueryRepository存储库{
@凌驾
PostDetailsQuerydToFindBypostId(UUID postId);
@凌驾
页面findAll(可分页可分页);
}
@Builder(toBuilder = true)
@EqualsAndHashCode
@ToString
@Getter
@AllArgsConstructor
public class PostDetailsQueryDTO implements Serializable {
private UUID postId;
private UUID ownerId;
private String title;
private String description;
private ZonedDateTime createDate;
}
interface SqlPostQueryRepository extends PostQueryRepository, Repository<PostSnapshot, Long> {
@Override
PostDetailsQueryDTO findByPostId(UUID postId);
@Override
Page<PostDetailsQueryDTO> findAll(Pageable pageable);
}