Reputation: 704
I have following structure:
@Component
public abstract class Entity<T> implements Serializable {
protected long id;
protected String entityType;
protected String entityKey;
protected Map<Language,EntityMetaData> metaData;
protected Object miscMetaData;
...
Class Event extends Entity and is a partial implementation of EAV pattern:
public class Event extends Entity<Long> implements Serializable {
public Event() {
}
private Account account;
private Entity<Long> entity;
private Currency currency;
private Double amount;
private Double units;
private Timestamp eventDate;
private Double unitPrice;
private Account customer;
XXEntity plays a role of discriminator (Id of Entity corresponds to Entity type):
public abstract class XXEntity extends Entity<Long> implements Serializable {
protected String value;
...
I want to map such inheritance in Hibernate but stuck with discriminator. Is that possible to use discriminator as Id of Entity class?
Here is my XML mappings of Entity & Event:
<hibernate-mapping default-lazy="false">
<class name="com.core.domain.abstracts.Entity" table="ENTITIES" dynamic-update="false" dynamic-insert="true" batch-size="30">
<id name="id" type="long" column="id">
<generator class="assigned" />
</id>
<property name="entityType" type="string" length="256">
<column name="entity" not-null="true"/>
</property>
<property name="entityKey" type="string" length="256">
<column name="key" not-null="true"/>
</property>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.core.domain" default-lazy="false">
<class name="Event" table="EVENTS" schema="EDRIVE" dynamic-update="false" dynamic-insert="true" batch-size="30">
<meta attribute="class-description">
This class contains definitions of relations between entities
</meta>
<id name="id" type="long" column="id">
<generator class="identity" />
</id>
<many-to-one name="account" column="accountId" class="com.core.domain.Account" not-null="true"/>
<many-to-one name="entity" column="entityId" class="com.core.domain.abstracts.Entity" not-null="true"/>
<discriminator column="entityType" type="com.core.domain.views.XXEntity"/>
<many-to-one name="currency" column="currencyId" class="com.core.domain.Currency" not-null="true"/>
<property name="amount" column="amount" type="java.lang.Double" not-null="true"/>
<property name="units" column="units" type="java.lang.Double" not-null="true"/>
<property name="eventDate" column="eventDate" type="java.sql.Timestamp" not-null="true"/>
<property name="unitPrice" column="unitPrice" type="java.lang.Double" not-null="true"/>
<many-to-one name="customer" column="customerId" class="com.core.domain.Account" not-null="true"/>
</class>
</hibernate-mapping>
Upvotes: 0
Views: 418
Reputation: 704
I solved the problem by using composition instead of inheritance. Account & Event classes don't correspond to each other, so, can't be participated in hierarchy like childs. Field Event.entity can be whatever class, so, it is difficult to map it into the particular entity.
Hence I decided to use several mappings for each type of entity, like AccountService, MileageEntity etc. All of them are extend abstract Entity, so, can be used in such pattern.
Upvotes: 0