问题
We're setting up a new project and decided to use eclipselink for JPA. When creating our domain model we ran into a problem.
We have a base class called organisation. We also have Supplier and Customer which both extend organisation. When JPA created the tables I saw that it uses a discriminator, the problem with this is that a supplier can also be a organisation.
So what I basically want is (these are database tables to get the idea):
A little example to help clarify this:
We have a Customer called SparklingGlass. SparklingGlass buys computers from us so SparklingGlass is saved as our Customer. We in turn buy our windows from SparklingGlass, so SparklingGlass is also our Supplier. This is what we want to realize in our system.
Is this in any way possible in JPA and ifnot, what is the best practice in these cases?
BTW we use the JOINED inheritance type
回答1:
Your data model is wrong. A Supplier is not a sub-type of Organization but is a Role played by an Organization.
An organization can play many roles, such as customer, vendor, supplier, employer.
回答2:
No sure I understand. In Java inheritance an instance can only be of one class. Since Supplier and Customer inherit from Organisation, "all" Suppliers and "all" Customers are Organisations.
Queries for Supplier will join both the Organisation and the Supplier tables to build Supplier instances.
来源:https://stackoverflow.com/questions/11862479/jpa-multiple-discriminator-values