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-name
s (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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With