I have to make the domain-layer of a collage project. We have few standards, like we have to use Hibernate and the database also is fix.
The relevant part of the database looks nearly like that:
BusEntitys (Table 1)
- BusId
- Bus specific information
BusType (Table 2)
- BusTypeId
- Seats
- ...
- SubClass Discriminator
The problem I have is that there are 2 types of buses in the domain-layer distinguish by the discriminator in the BusType-table:
@Entity
class Bus
{
@Id
int _id;
...
}
@Entity
class BusSubClass1 extends Bus
{
...
}
@Entity
class BusSubClass2 extends Bus
{
...
}
Is there a way to map something like this with Hibernate and JPA? Thanks for every answer.
Jochen Morent
Yes, there are few ways to map such scenario. JPA provides support for three different data representations:
In other words depending on inheritance type used you will get different database model. Various JPA providers may support additional inheritance strategies.
Consider the following example:
Using
InheritanceType.SINGLE_TABLE
strategy leads to a single database table containing all concrete entity types:Using
InheritanceType.JOINED
strategy leads to multiple database tables per entity type (all shared fields fromBus
are stored in the corresponding table):Using
InheritanceType.TABLE_PER_CLASS
strategy leads to exqactly one database table per entity type (all shared fields fromBus
are redefined in the concrete subclasses):