gpt4 book ai didi

hibernate - Spring Data DTO 投影

转载 作者:行者123 更新时间:2023-12-01 01:48:36 27 4
gpt4 key购买 nike

我们想在界面一上使用 DTO 投影,因此我们创建了以下 DTO 对象:

人事部

@Data
@Builder
@AllArgsConstructor
public class PersonDto {

private String name;

private String email;

private AddressDto address;

}

地址Dto
@Data
@Builder
@AllArgsConstructor
public class AddressDto {

private String address;

private String streetNumber;

}

一个仓库
@Repository
public interface PersonRepository extends JpaRepository<PersonEntity, Long> {

List<PersonDto> findAllDtoedBy();

}

但是,当被调用时,我们得到了异常:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.example.projections.model.PersonDto]. Expected arguments are: java.lang.String, java.lang.String, com.example.projections.model.AddressEntity [select new com.example.projections.model.PersonDto(generatedAlias0.name, generatedAlias0.email, address) from com.example.projections.model.PersonEntity as generatedAlias0 left join generatedAlias0.address as address]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]

请注意这一行:“...预期的参数是:java.lang.String、java.lang.String、com.example.projections.model。 AddressEntity ...”。

显然,它需要模型对象 AddressEntity,并且在找到 DTO 对象 (AddressDto) 时会感到困惑。

所以问题是,由于这适用于界面投影,我们希望 DTO 投影也支持它?
通过查看 GitHub ( https://github.com/spring-projects/spring-data-examples/blob/master/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java ) 和 Spring Data JPA 文档中的示例,我们没有看到此案例的示例,但我们也没有看到此 的明确声明。不是 支持的

最佳答案

来自 Spring Data JPA - Reference Documentation :

Another way of defining projections is using value type DTOs that hold properties for the fields that are supposed to be retrieved. These DTO types can be used exactly the same way projection interfaces are used, except that no proxying is going on here and no nested projections can be applied.



我想原因是这个...

关于hibernate - Spring Data DTO 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46388363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com