是否有一个 ORM 支持组合 w/o Joins
编辑:将标题从“继承”更改为“组合”。问题正文保持不变。
我很好奇是否有一个 ORM 工具支持继承,而无需创建必须连接的单独表。
简单的例子。假设有一个包含收单地址的客户表和一个包含汇款地址的供应商表。保持简单并假设每个地址都有一个地址,而不是每个地址都有一个子地址表。
这些地址将具有一些共同值:地址 1、地址 2、城市、州/省、邮政编码。假设我有一个类“addressBlock”,并且我希望客户和供应商从该类继承,也可能从其他类继承。但我不想要必须连接的单独表,我想要分别在客户和供应商表中的列。
有支持这个的 ORM 吗?
我在 StackOverflow 上找到的最接近的问题可能是同一问题,如下链接,但我不太清楚 OP 是否在问我在问什么。他似乎正在询问前述继承,正是因为会有多个表。我正在寻找可以使用继承而不生成多个表的情况。
EDIT: Changed title from "inheritance" to "composition". Left body of question unchanged.
I'm curious if there is an ORM tool that supports inheritance w/o creating separate tables that have to be joined.
Simple example. Assume a table of customers, with a Bill-to address, and a table of vendors, with a remit-to address. Keep it simple and assume one address each, not a child table of addresses for each.
These addresses will have a handful of values in common: address 1, address 2, city, state/province, postal code. So let's say I'd have a class "addressBlock" and I want the customers and vendors to inherit from this class, and possibly from other classes. But I do not want separate tables that have to be joined, I want the columns in the customer and vendor tables respectively.
Is there an ORM that supports this?
The closest question I have found on StackOverflow that might be the same question is linked below, but I can't quite figure if the OP is asking what I am asking. He seems to be asking about foregoing inheritance precisely because there will be multiple tables. I'm looking for the case where you can use inheritance w/o generating the multiple tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPA(Java Persistence API,由 Hibernate、EclipseLink 等实现)支持这一点:您可以定义一个 Address POJO,将其标记为“可嵌入”,并有两个实体(客户和供应商),每个实体都有一个 Address 类型的字段(标记为“嵌入”)。客户地址的字段将映射到客户表中的列,供应商地址的字段将映射到供应商表中的列。
请注意,这里没有继承。继承适用于is-a关系。供应商不是地址,客户也不是地址。不过,存在组合,因为供应商有地址,而客户有地址。
JPA (Java Persistence API, implemented by Hibernate, EclipseLink and others) supports this : You would define an Address POJO, mark it as "embeddable", and have two entities (Customer and Vendor) having each a field of type Address (marked as "embedded"). The fields of the customer address would be mapped to columns in the Customer table, and the fields of the vendor address would be mapped to columns in the Vendor table.
Note that there's no inheritance here. Inheritance is for is-a relationships. A vendor is not an address, and a customer is not an address either. There is composition, though, because a vendor has an address, and a customer has an address.