Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate same class with 2 entity-names to Spring Data JPA?

I am migrating an app to Spring Data JPA from Hibernate. I already migrated a few repositories and have that working. I now have a special case I need to convert.

I have this in my .hbm.xml:

<class name="SoundNotification" table="SoundNotification" entity-name="SoundNotificationWithData">
        <id name="m_id" type="int" column="id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="m_name" column="name" unique="true" not-null="true"/>
        <property name="m_data" column="data"
                  type="com.traficon.tmsng.server.common.service.persistence.impl.hibernate.usertype.BlobUserType"
                  not-null="true"/>
        <property name="m_size" formula="OCTET_LENGTH(data)"/>
        <property name="m_inUse"
                  formula="(select count(1) from EventTypeConfiguration etc where etc.soundNotification=id)"/>
    </class>

    <class name="SoundNotification" table="SoundNotification" entity-name="SoundNotificationWithoutData">
        <id name="m_id" type="int" column="id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="m_name" column="name" unique="true" not-null="true"/>
        <property name="m_size" formula="OCTET_LENGTH(data)"/>
        <property name="m_inUse"
                  formula="(select count(1) from EventTypeConfiguration etc where etc.soundNotification=id)"/>
    </class>

Notice how I only have 1 class SoundNotification, but it is used with 2 different entity-names (SoundNotificationWithData and SoundNotificationWithoutData)

Is it possible to convert this to Spring Data JPA? Would I need to create 2 java classes as a "workaround" ?

Another example which we have is this one:

<class name="FlowDataMessageImpl" entity-name="FlowDataPer10s" table="FlowDataPer10s">
...
</class>

<class name="FlowDataMessageImpl" entity-name="FlowDataPer20s" table="FlowDataPer20s">
....
</class>
<class name="FlowDataMessageImpl" entity-name="FlowDataPer2m" table="FlowDataPer2m">
...
</class>

Here we store the same "Java object" in different tables after we did some roll-up calculations. I would like to map this using JPA (or somebody to tell me it is a bad idea and I should use Hibernate directly like before for this)

like image 657
Wim Deblauwe Avatar asked Jan 23 '15 16:01

Wim Deblauwe


1 Answers

To your first question: you will have to create two Java classes SoundNotificationWithoutData and SoundNotificationWithData, both classes extending the same third Java class, using the @Inheritance(strategy=InheritanceType.SINGLE_TABLE) and mapped with the @Table(name="SoundNotification") annotation. Also notice that you will not be able in plain JPA to create a property with a formula (property m_inUse), so you will have to use Hibernate-specific stuff OR load that property only when you need it.

To your second problem: again, either use Hibernate-specific stuff, OR use the @MappedSuperclass annotation on the superclass (which is extended by every FlowDataPer* classes), without using on it the @Entity and @Inheritance annotations. Of course you could also use the same solution as for your first question: different classes (FlowDataPer10s, FlowDataPer20s, ..) extending a base entity class, anntoated with @Entity and @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS), but I find it more elegant with the @MappedSuperclass annotation.

like image 118
V G Avatar answered Sep 30 '22 18:09

V G