[英]Migrating Hibernate mixed inheritance from hbm.xml to orm.xml
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
考虑以下 Java 层次结构:
我希望整个层次结构存储在一个名为file
的表中,除了SpecialOutFile
应该有其专用的special_file
表。
这使用 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>
当我坚持一个OutFile
时,只插入一条记录,在file
中。
当我保留一个SpecialOutFile
时,只插入一条记录,在special_file
中。
现在我想在 orm.xml 中做同样的事情,因为 hbm.xml 在 Hibernate 6 中被弃用了。但是,我无法重现完全相同的行为。
我能得到的最接近的是使用辅助表,但现在当我坚持使用SpecialOutFile
时,会创建两条记录:一条在file
中,一条在special_file
中:
Hibernate: insert into file (some_data, type, id) values (?, 'SpecialOutFile', ?)
Hibernate: insert into special_file (more_data, id) values (?, ?)
这是我当前版本的 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>
有没有办法将所有继承的属性移动到辅助表中,并在保留SpecialOutFile
时完全摆脱主表,就像我能够使用 hbm.xml 映射一样?
我认为您正在寻找的是基于鉴别器的连接 inheritance: https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_joined_inheritance_XML
您只需为每个 class 提及相同的表名, SpecialOutFile
除外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.