gpt4 book ai didi

java - 是否可以在没有实体的情况下使用 JpaRepository?

转载 作者:行者123 更新时间:2023-12-05 01:05:03 33 4
gpt4 key购买 nike

是否可以在没有实体的情况下使用 JpaRepository?在这种情况下,将其替换为 DTO。

如下示例

 @Repository
public interface BffRepository extends JpaRepository<BffDTO, String> {

@Query(nativeQuery = true, value = "select\n"
+ "ent.name as enterprise_name, dep.name as department_name,\n"
+ "sq.name as squad_name, acc.firstname as job_owner_name,\n"
+ "tpt.name as test_template_name, job.name, job.job_blocked, job.job_removed,\n"
+ "job.bot_scm_branch, job.bot_scm_url, job.schedule_startdate,\n"
+ "job.expiration_date, job.timestamp,job.uuid,job.schedule_starttime,\n"
+ "tpt.job_execution_timeout\n"
+ "from portal.jobs job\n"
+ "left join portal.enterprises ent on (ent.uuid = job.enterprise_id)\n"
+ "left join portal.departments dep on (dep.uuid = job.department_id)\n"
+ "left join portal.squads sq on (sq.uuid = job.squad_id)\n"
+ "left join portal.accounts acc on (acc.uuid = job.job_owner)\n"
+ "left join portal.test_plan_templates tpt on (tpt.uuid = job.template_id) where\n"
+ "job.job_owner = ?1 and job.job_removed = false order by timestamp desc;")
List<BffDTO>buscarPorJobOwner(String jobOwner);

这种情况有替代方案吗?

注意:DTO 已经映射,但我不想创建一个 View 来将此 DTO 转换为实体。

我已经验证了这个主题,但没有取得重大进展 Use JpaRepository interaction style without entity

我正在尝试这个

界面-

公共(public)接口(interface) BffDTOInterface2 {

String uuid();

String enterprise_name();

String department_name();

String squad_name();

String job_owner_name();

String test_template_name();

String name();

Boolean job_blocked();

Boolean job_removed();

String bot_scm_branch();

String bot_scm_url();

String schedule_startdate();

String expiration_date();

String timestamp();

String schedule_starttime();

Integer job_execution_timeout();

@Transient
String status();

}

我遇到了这个错误

Caused by: java.lang.IllegalArgumentException: Not a managed type: interface br.com.cloud.api.domain.dto.BffDTOInterface2

最佳答案

您可以使用基于接口(interface)的投影。

例如

  1. 创建您的 native 查询,为其列一个别名。 select name as fullName, age as age from person.

  2. 创建一个接口(interface)来代表您的 DTO,并为您的 native 查询的每个别名提供 get 方法。

interface MyDTO {
String getFullName();
Integer getAge();
}
  1. 您的查询的返回类型现在可以是这个 MyDTO
@Query(value = "select name as fullName, age as age from person", nativeQuery=true)
List<MyDTO> findMyDTO();

关于java - 是否可以在没有实体的情况下使用 JpaRepository?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71177494/

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