Reputation: 977
in our system we store orders. We always have customer specific code in our projects, but we try to standardize as much as possible. Now I'm setting up a test with Hibernate to see if we can use it to improve our Data Access Object(s). I have some issues understanding the possibilities of inheritance with Hibernate. Let me outline the issue:
We have 4 layers of software:
In the core library we have the following model regarding Orders
Lets assume an Order has the following default fields:
In the customer specific library we have the following regarding Order:
Now a customer ordering our system also wants to store the country and priority in addition to the above fields. The customer specific fields would be:
The application loads the correct Order class (either Order or CustomerOrder) via a factory. The factory knows somehow if there is a customer specific implementation.
In de database we would like to have 1 table (for performance reasons), our core / default table (so without any customer specific extension) would look like this:
create table ORDER
(
OR_ID VARCHAR2(10) not null constraint OR_ID_PK primary key,
OR_CUSTOMER_NAME VARCHAR2(20),
OR_ORDER_DATE datetime,
OR_DELIVERY_DATE datetime
);
For the customer which requires an extra two fields we would create the same table as:
create table ORDER
(
OR_ID VARCHAR2(10) not null constraint OR_ID_PK primary key,
OR_CUSTOMER_NAME VARCHAR2(20),
OR_ORDER_DATE datetime,
OR_DELIVERY_DATE datetime,
OR_COUNTRY VARCHAR2(20),
OR_PRIORITY NUMBER
);
Now my problem is how I can model this in Java using Hibernate using only 1 table. The core Order entity does not know about any extra columns that are available in the CustomerOrder. I investigated some options on Hibernate in combination with inheritance, for example using strategy InheritanceType.SINGLE_TABLE. But I believe that requires a discriminator column, which I don't have.
What are my options here with Hibernate?
Upvotes: 1
Views: 550
Reputation: 9306
I don't know if I got it right but it seems that you have two classes Order and CustomerOrder which inherits Order. If this is true then single table strategy fits well without any configuration and the discriminator column is the class name, so if the record is belong to Order you'll got Order in as discriminator value and if it is CustomerOrder you'll got CustomerOrder as discriminator value. This is the default behavior for hibernate and you can modify it.
Upvotes: 1
Reputation: 692121
In this case, you don't ever have two kinds of entities at once (Order and CustomerOrder). You have only Orders if there is no customization, and you have only CustomerOrders if there is customization.
And in fact, you could assume that you always have only CustomerOrder, but that this class is not entirely under your control, and could be replaced by another one from the customer.
So I would probably make Order a MappedSuperclass (i.e. a class which does not constitute an entity, but has persistent fields that are inherited by entities).
In the extension jar, that could be customized by the customer, I would define the CustomerOrder entity, which would just extend Order and not add any other field. This CustomerOrder class would be available in source form to customers, which could replace it with their own implementation with additional fields.
Upvotes: 1