是否可以在没有实体的情况下使用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已经映射,但我不想创建视图来将此DTO转换为实体。
我已经验证了这个主题,但没有重大进展,请使用无实体的JpaRepository交互样式
我在试这个
接口-
公共接口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
但你可以试试这个。
您可以做的是您可以创建自己的自定义存储库类。首先,您将拥有一些调用存储库类的服务类。还请注意,我们为SQL查询的结果集提供了自定义模型。
@Service
public class CustomService {
@Autowired
private CustomRepository repository;
public List<CustomResponse> getAllResult(String id) {
List<Object[]> items = repository.getAllResult(id);
List<CustomResponse> customResponseList = new ArrayList();
for (Object[] item: items) {
CustomResponse c = new CustomResponse();
c.setTestValue1(String.valueOf(item[0]));
c.setTestValue2(String.valueOf(item[1]));
customResponseList.add(c);
}
return customResponseList;
}
}
您的存储库类将如下所示。
@Repository
public class CustomRepository {
@Autowired
private EntityManager entityManager;
public List<Object[]> getAllResult(String id) {
Query q = (Query) entityManager.createNativeQuery("SELECT\n" +
" users.user_id as user_id,\n" +
" users.email as user_email\n" +
" FROM Users\n" +
" WHERE users.parent_id = :parent_id;");
q.setParameter("parent_id", id);
List<Object[]> results = q.getResultList();
return results;
}
}
此外,您可能希望拥有自己的模型。(
public class CustomResponse {
private String testValue1;
private String testValue2;
public String getTestValue1() {
return testValue1;
}
public void setTestValue1(String testValue1) {
this.testValue1 = testValue1;
}
public String getTestValue2() {
return testValue2;
}
public void setTestValue2(String testValue2) {
this.testValue2 = testValue2;
}
}
可以使用基于接口的投影。
例如
>
创建本机查询givin it列别名<代码>选择姓名作为全名,年龄作为年龄来自个人。
创建一个接口,该接口使用get-method表示本机查询的每个别名的DTO。
interface MyDTO {
String getFullName();
Integer getAge();
}
@Query(value = "select name as fullName, age as age from person", nativeQuery=true)
List<MyDTO> findMyDTO();
是否可以在没有实体的情况下使用JpaRepository?
不,它不是,根据定义,它将完全违背JPA的目的。
JPA是启用ORM的持久性规范——对象关系映射——也就是说,您将Java对象分别映射到数据库表的条目/行,并将Java类型分别映射到数据库表。
DTO(数据传输对象)与ORM无关,它有不同的用途(我建议您阅读本文了解DTO与实体的关系)-通过Java对象传输数据-它通常服务于中间层,用于将持久对象(@实体)转换为要在web层(DTO)中使用的对象,反之亦然。
如果您真的想避免持久层模型(
@Entity
s),您可以选择JDBC抽象(例如Spring数据JDBC)、本机查询、JPQL、HQL或纯JDBC API(我不推荐)。
我想使用并使其直接进入给定的url,而不是从ribbon配置中获取主机。 我知道在Spring,cloud-feign默认与ribbon和eureka一起出现。 根据这个:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html#spring-cloud-ribbon-without-eure
在Spring Boot的文档中,我只找到了使用Redis会话的例子,不使用Redis也能使用它吗?
问题内容: 我正在尝试创建一个与SurveyMonkey API交互的基于Java的本地客户端。 SurveyMonkey需要使用OAuth 2.0的长期访问令牌,我对此不太熟悉。 我已经搜索了几个小时,但我认为答案是否定的,但我只想确定一下: 我是否可以编写一个与SurveyMonkey交互的简单Java客户端, 而无需在某些云中设置自己的重定向服务器 ? 我觉得必须拥有自己的在线服务才能接收O
我已经从源代码处构建并安装了另一个glibc,并且我想让现有的用C++编写的可执行文件与自定义glibc一起运行,以供实验之用。为了做到这一点,我尝试更改可执行文件的加载程序。首先,在/lib64下创建了一个名为的链接,其路径指向新的加载程序 其次,通过文本编辑器修改了可执行文件中的加载器路径,将“/lib64/ld-linux-x86-64.so.2”更改为“/lib64/ld_linux-x8
我们有一个架构体系,有自己的应用编程接口网关、服务发现和负载平衡。然而,出于恢复目的,我需要使用Hystrix。 与Spring的云Netflix,可以Hystrix(即。断路器注释)在没有Eureka/Ribbon或其他NetflixOSS模块的情况下使用? 是否有任何依赖于Eureka/Ribbon/Zuul的断路器仪表板(即涡轮机和流聚合器)? 断路器注释可以在非Spring启动应用程序中使
问题内容: 如果要使用Linq-SQL,还必须将DB Table拖到设计器表面以创建实体类。 我一直喜欢我的应用程序中的完全控制权,并且不喜欢dotnet创建的类。 是否可以使用我自己的数据访问层实体类在Linq和DB之间提供此连接? 我该如何完成? 问题答案: 您可以使用Linq-to-SQL非常轻松地编写自己的类-只需使用一些属性绘制类即可。 例如,这是我的一个项目中有一个非常简单的表,它可以