Spring Data JDBC支持实体之间的联接吗?

Ser*_*lov 3 java spring spring-data-jpa spring-boot spring-data-jdbc

正如我在文档中所读到的,Spring Data JDBC支持像Spring Data JPA这样的查询创建

例如: findByProperty(Property property)

我的问题 :

Spring Data JDBC是否支持以下情况:我们创建查询并使用两个(或多个)实体的属性将它们连接起来以像Spring Data JPA中那样查找结果?

例:

@Entity
class Person {
  private final @Id Long id;
  private final Car car;
}

@Entity
class Car {
  private final @Id Long id;
  private String color;
}

interface PersonRepository extends CrudRepository<Person, Long> {
  List<Person> findByCarColor(Color red);
}

interface CarRepository extends CrudRepository<Car, Long> {
}
Run Code Online (Sandbox Code Playgroud)

我想找到所有至少拥有一辆红色轿车的人。该方法会提供适当的输出吗?

Jen*_*der 7

恐怕您误读了文档。

Spring Data JDBC 1.0版本不支持查询派生。它肯定会在不久的将来添加。

造成误解的原因是,所有Spring Data文档均以概括性部分开头,概述了原则上可用于模块的功能。这部分对于所有模块都是相同的。然后是模块的特定部分,描述了实际功能。不幸的是,不支持查询派生的事实只能从模块特定部分未提及的事实中推论得出。

一旦此功能到达,它很可能将支持跨实体的查询,但至少在开始时仅跨相同聚合的实体进行查询。

聚合的概念对于Spring Data JDBC极为重要,这就是为什么有一篇博客文章对此概念及其对Spring Data JDBC的影响的原因,我强烈建议阅读。