I am working with a domain model class which is composed of an embedded object which implements the DomainEvent interface. DomainEvent can be any concrete class and is serialized by Jackson. To solve this in hibernate 5 I have used a CompositeUserType over two columns: event_body, and type_name.
public class StoredEvent {
private DomainEvent event;
private long eventId;
private Instant occurredOn;
private String typeName;
}
In Hibernate 5 the hbm.xml mapping was very simple and straightforward:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" default-cascade="all">
<class name="event.StoredEvent" table="stored_event" lazy="true">
<id name="eventId" type="long" column="event_id" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="event" type="port.adapter.persistence.hibernate.DomainEventUserType">
<column name="event_body"/>
<column name="type_name"/>
</property>
<property name="typeName" column="type_name" insert="false" update="false"/>
<property name="occurredOn" column="occurred_on"/>
</class>
</hibernate-mapping>
|