简体   繁体   English

Spring 数据 r2dbc - 实体 inheritance

[英]Spring Data r2dbc - Entity inheritance

Spring data JPA with Hibernate supports annotiation javax.persistence.Inheritance and constants inside javax.persistence.InheritanceType . Spring data JPA with Hibernate supports annotiation javax.persistence.Inheritance and constants inside javax.persistence.InheritanceType . Based on these, inheritance between entity classes is mapped into DB (single table/joined table https://stackoverflow.com/a/3579462/12053054 ).基于这些,将实体类之间的 inheritance 映射到 DB(单表/连接表https://stackoverflow.com/a/3579462/12053054 )。

I was not able to find any similar mechanism to support entity inheritance with spring data r2dbc.我找不到任何类似的机制来支持实体 inheritance 和 spring 数据 r2dbc。 I know that JPA and Hibernate stuff has nothing to do with r2dbc, but i don't see any problem with supporting inheritance in spring data r2dbc repositories nor any "anti-pattern" against functional programming. I know that JPA and Hibernate stuff has nothing to do with r2dbc, but i don't see any problem with supporting inheritance in spring data r2dbc repositories nor any "anti-pattern" against functional programming.

Is there any workaround for this or any mechanism that would let me to use inheritance in spring data r2dbc repositories?是否有任何解决方法或任何机制可以让我在 spring 数据 r2dbc 存储库中使用 inheritance? (not just inheriting fields, but also translating inheritance to DB same as JPA/Hibernate does when calling spring data JPA repository methods). (不仅继承字段,而且将 inheritance 转换为 DB,与 JPA/Hibernate 在调用 spring 数据 JPA 存储库方法时所做的相同)。 My only temporary workaround for this is to manually execute queries and implement spring data r2dbc repository methods on my own so i can translate inheritance into DB as well.我唯一的临时解决方法是手动执行查询并自行实施 spring 数据 r2dbc 存储库方法,这样我也可以将 inheritance 转换为 DB。

r2dbc is not an ORM (ie no OBJECT relational mapper) and hence the hole topic of object orientation is not supported. r2dbc 不是 ORM(即没有 OBJECT 关系映射器),因此不支持 object 方向的孔主题。 On a pure SQL basis there is no out-of-the-box inheritance, but you need to build your tables yourself that represent inheritance (but technically speaking they are just tables).在纯 SQL 的基础上,没有开箱即用的 inheritance,但您需要自己构建代表inheritance 的表格(但从技术上讲,它们只是表格)。

Classical approach: table mapping strategies经典方法:表映射策略

Until there is any ORM for r2dbc and the spring reactive stack you will need to implement the inheritance mapping to the tables yourself as well as the repositories that join the tables according to your mapping strategy.直到有任何 ORM 用于 r2dbc 和 spring 反应堆栈,您将需要实施 inheritance 根据您自己的映射策略连接到表的表。 Most common strategies (with each having pros and cons) are:最常见的策略(各有利弊)是:

  • Single table (merge all columns into a single table)单表(将所有列合并到一个表中)
  • Concrete table (repeat all common columns per concrete table)混凝土表(重复每个混凝土表的所有公共列)
  • Table per type (have a table holding common columns and then a table per concrete class with their added columns only)每个类型的表(有一个包含公共列的表,然后每个具体 class 的表以及它们添加的列)

Find more details on google or for example in this answer .在 google 上或例如在此答案中查找更多详细信息。

Alternative: schema-less替代方案:无模式

One other approach might be to use normal columns for the common attributes and a schema-less column for the data of subclasses.另一种方法可能是对公共属性使用普通列,对子类的数据使用无模式列。 For example, when using postgres, it has very good support for the JSONB type that you could use to easily implement the repositories without a lot of joins etc.例如,当使用 postgres 时,它对 JSONB 类型有很好的支持,您可以使用它轻松实现存储库而无需大量连接等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM