Wim*_*uwe 6 java hibernate jpa spring-data
我正在从Hibernate将应用程序迁移到Spring Data JPA.我已经迁移了一些存储库并使其正常工作.我现在有一个需要转换的特殊情况.
我在.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>
Run Code Online (Sandbox Code Playgroud)
请注意我只有1个类SoundNotification,但它与2个不同的entity-names一起使用(SoundNotificationWithData和SoundNotificationWithoutData)
是否可以将其转换为Spring Data JPA?我需要创建2个java类作为"解决方法"吗?
我们的另一个例子就是这个:
<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>
Run Code Online (Sandbox Code Playgroud)
在我们进行一些汇总计算之后,我们在不同的表中存储相同的"Java对象".我想用JPA来映射这个(或者有人告诉我这是一个坏主意,我应该像以前一样直接使用Hibernate)
对于第一个问题:您必须创建两个 Java 类SoundNotificationWithoutData和SoundNotificationWithData,这两个类都扩展同一个第三个 Java 类,并使用@Inheritance(strategy=InheritanceType.SINGLE_TABLE)和 与@Table(name="SoundNotification")注释进行映射。另请注意,您将无法在普通 JPA 中使用公式 (property m_inUse) 创建属性,因此您必须使用 Hibernate 特定的内容或仅在需要时加载该属性。
对于第二个问题:同样,要么使用 Hibernate 特定的东西,要么在超类上使用@MappedSuperclass注释(由每个FlowDataPer*类扩展),而不使用@Entity和@Inheritance注释。当然,您也可以使用与第一个问题相同的解决方案:不同的类(FlowDataPer10s、FlowDataPer20s、 ..)扩展基实体类,并用@Entity和进行@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)注释,但我发现使用注释更优雅@MappedSuperclass。