0

如何在 Spring Data R2DBC 中使用地图,将具有接收/发送关系的地图表/实体发送到数据库?使用 r2dbcCustomConversions、@WritingConverter 和 @ReadingConverter。有人可以举个例子吗?非常感谢

4

1 回答 1

-1

也许lc-spring-data-r2dbc可以帮助你,它支持加载和保存具有关系的实体。

例如,您可以声明这样的链接:

@Table
public class TableWithForeignKey {
  ...
  @ForeignKey(optional = false)
  private LinkedTable myLink;
  ...
}

@Table
public class LinkedTable {
  ...
  @ForeignTable(joinkey = "myLink")
  private List<TableWithForeignKey> links;
  ...
}

当您希望链接被映射时,您可以使用延迟加载或连接选择。

如果使用findBy...Spring 存储库的方法(findById、findAll...),则只会加载存储库的表。在这种情况下,您可以使用延迟加载。为此,您需要声明一个具有默认主体的方法,并且您的方法将自动实现:

  public Flux<TableWithForeignKey> lazyGetLinks() {
    return null; // will be implemented
  }

另一种方法是直接在请求中进行连接。目前不支持在存储库中自动进行连接(如 JPA 中的 @EntitiGraph),但您可以像这样实现您的方法:

public interface MyRepository extends LcR2dbcRepository<LinkedTable, Long> {
  
  default Flux<LinkedTable> findAllAndJoin() {
    SelectQuery.from(LinkedTable.class, "root") // SELECT FROM LinkedTable AS root
      .join("root", "links", "link")            // JOIN root.links AS link
      .execute(getLcClient());                  // execute the select and map entities
  }

}

结果将是所有 LinkedTable 实例,以及从数据库一起加载的链接列表。

于 2020-12-01T17:16:18.027 回答