1

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.

Model inheritance approach with Django's ORM

Community
  • 1
  • 1
Ken Downs
  • 4,707
  • 1
  • 22
  • 20
  • My immediate thought is that you're using inheritance where you really should be using composition. – btilly Feb 05 '11 at 15:02
  • What exactly are you afraid of, with the Address being Normalised into a separate Address table, why do you **not want it**. The nature of databases is many small tables, SQL is designed for joins. – PerformanceDBA Feb 06 '11 at 10:37

1 Answers1

4

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I'm not clear from your answer if an "embedded Address" contains a collection of columns or just one column. In other words, do you define a collection and embed it once, or must you embed each column? – Ken Downs Feb 05 '11 at 20:01
  • an embedded address contains as many fields as you want, each mapped to its own column. The address itself contains the mapping of fields to columns, but each time an address is actually embedded, the embedding entity (for example the Customer or the vendor) is free to customize the mapping. See http://www.java2s.com/Tutorial/Java/0355__JPA/ShareEmbeddableEntity.htm for an example – JB Nizet Feb 06 '11 at 11:58