[英]Migrating Hibernate mixed inheritance from hbm.xml to orm.xml
Consider the following Java hierarchy:考虑以下 Java 层次结构:
I want the whole hierarchy to be stored in a single table named file
, except for SpecialOutFile
which should have its dedicated special_file
table.我希望整个层次结构存储在一个名为
file
的表中,除了SpecialOutFile
应该有其专用的special_file
表。
This works perfectly well using hbm.xml mappings:这使用 hbm.xml 映射非常有效:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
default-access="field"
default-cascade="all"
default-lazy="true">
<class name="com.example.demo.File">
<id name="id"/>
<discriminator column="file_type"/>
<subclass name="com.example.demo.InFile" discriminator-value="InFile"/>
<subclass name="com.example.demo.TypedOutFile1" discriminator-value="TypedOutFile1"/>
<subclass name="com.example.demo.TypedOutFile2" discriminator-value="TypedOutFile2"/>
<subclass name="com.example.demo.OutFile" discriminator-value="OutFile"/>
</class>
<class name="com.example.demo.SpecialOutFile" table="special_file">
<id name="id"/>
<property name="moreData"/>
</class>
</hibernate-mapping>
When I persist an OutFile
, only one record is inserted, in file
.当我坚持一个
OutFile
时,只插入一条记录,在file
中。
When I persist a SpecialOutFile
, only one record is inserted, in special_file
.当我保留一个
SpecialOutFile
时,只插入一条记录,在special_file
中。
Now I'd like to do the same in orm.xml, as hbm.xml became deprecated in Hibernate 6. However, I cannot manage to reproduce the exact same behavior.现在我想在 orm.xml 中做同样的事情,因为 hbm.xml 在 Hibernate 6 中被弃用了。但是,我无法重现完全相同的行为。
The closest I can get is using a secondary table, but now when I persist a SpecialOutFile
two records are created: one in file
and one in special_file
:我能得到的最接近的是使用辅助表,但现在当我坚持使用
SpecialOutFile
时,会创建两条记录:一条在file
中,一条在special_file
中:
Hibernate: insert into file (some_data, type, id) values (?, 'SpecialOutFile', ?)
Hibernate: insert into special_file (more_data, id) values (?, ?)
Here's my current version of orm.xml:这是我当前版本的 orm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<access>FIELD</access>
<entity class="com.example.demo.File">
<discriminator-column name="type"/>
<attributes>
<id name="id"/>
</attributes>
</entity>
<entity class="com.example.demo.InFile">
<discriminator-value>InFile</discriminator-value>
</entity>
<entity class="com.example.demo.TypedOutFile1">
<discriminator-value>TypedOutFile1</discriminator-value>
</entity>
<entity class="com.example.demo.TypedOutFile2">
<discriminator-value>TypedOutFile2</discriminator-value>
</entity>
<entity class="com.example.demo.OutFile">
<discriminator-value>OutFile</discriminator-value>
</entity>
<entity class="com.example.demo.SpecialOutFile">
<secondary-table name="special_file"/>
<attribute-override name="id">
<column table="special_file"/>
</attribute-override>
<attributes>
<basic name="moreData">
<column table="special_file"/>
</basic>
</attributes>
</entity>
</entity-mappings>
Is there a way to move all inherited attributes into the secondary table and completely get rid of the primary table when persisting a SpecialOutFile
, like I was able to do with hbm.xml mappings?有没有办法将所有继承的属性移动到辅助表中,并在保留
SpecialOutFile
时完全摆脱主表,就像我能够使用 hbm.xml 映射一样?
Minimal reproducible exemple here: https://github.com/bjansen/so-question-75328749这里的最小可重现示例:https://github.com/bjansen/so-question-75328749
I think what you are looking for is discriminator based joined inheritance: https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_joined_inheritance_XML我认为您正在寻找的是基于鉴别器的连接 inheritance: https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_joined_inheritance_XML
You'd just mention the same table name for every class except for the SpecialOutFile
one.您只需为每个 class 提及相同的表名,
SpecialOutFile
除外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.