Spring boot 在Spring数据Jpa中使用DTO的基于类的投影不起作用
在我的Spring Boot应用程序中,我尝试使用DTO实现基于类的投影,如所述: 我有一个域类,看起来像:Spring boot 在Spring数据Jpa中使用DTO的基于类的投影不起作用,spring-boot,spring-mvc,spring-data-jpa,spring-data-rest,Spring Boot,Spring Mvc,Spring Data Jpa,Spring Data Rest,在我的Spring Boot应用程序中,我尝试使用DTO实现基于类的投影,如所述: 我有一个域类,看起来像: @Entity @Table(name="metadatavalue") public class MetadataValue { @Id @Column(name="metadata_value_id") private Integer metadataValueId; @Column(name="resource_id", insertable=
@Entity
@Table(name="metadatavalue")
public class MetadataValue {
@Id
@Column(name="metadata_value_id")
private Integer metadataValueId;
@Column(name="resource_id", insertable=false, updatable=false)
private Integer resourceId;
@Column(name="metadata_field_id", insertable=false, updatable=false)
private Integer metadataFieldId;
@Column(name="text_value")
private String textValue;
更多的类成员、getter和setter等随之出现
我还有一个简单的DTO类,只有一个成员:
public class MetadataDTO {
private String textValue;
public MetadataDTO(String textValue) {
this.textValue = textValue;
}
public String getTextValue() {
return textValue;
}
}
我的存储库类是:
public interface MetadataValueRepository extends CrudRepository<MetadataValue, Integer> {
@Query("SELECT m from MetadataValue m WHERE m.handle.handle = :handle AND m.resourceTypeId=m.handle.resourceTypeId")
List<MetadataValue> findAllByHandle(@Param("handle") String handle);
@Query("SELECT new path.to.my.package.domain.MetadataDTO(m.textValue AS textValue) FROM MetadataValue m "
+ "WHERE m.handle.handle = :handle AND m.resourceTypeId=m.handle.resourceTypeId")
List<MetadataDTO> findAllByHandleDTO(@Param("handle") String handle);
}
我也尝试过从JpaRepository扩展,得到了同样的结果。在另一次尝试中,我尝试使用一个基于接口的投影,它产生了一个几乎相同的stacktrace,用一个内部类替换我的类
我的项目是SpringBoot2.3.0,包含SpringWeb、SpringDataJPA、Rest存储库和PostgreSQL驱动程序
有谁能帮我理解我做错了什么,让我走上正确的方向
非常感谢
关于这个问题的更新:(我已经添加了标签spring data rest)。通过实验,我了解到我可以成功地调用我的方法,例如,在我的应用程序类的run()方法中。只有当我将该方法作为rest端点调用时,我才会看到错误。这仅仅是一个与Spring数据Rest不兼容的问题吗?您不需要构造函数表达式,只需使用
选择m.textValue作为textValue
尝试使用Spring数据elasticsearch。如果我用@Document注释dto类的方式与JPA@Entity中实体本身的方式类似,那么我实际上可以工作。您不需要构造函数表达式,只需使用SELECT m.textValue as textValue
尝试使用Spring data elasticsearch相同。如果我用@Document对dto类进行注释的方式与JPA@Entity中实体本身的注释方式类似,那么实际上我是可以工作的
java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class path.to.my.package.domain.MetadataDTO!