简体   繁体   English

使用每个层次结构的表时,将同一数据库列映射到Hibernate中子类的不同字段/属性

[英]Mapping same DB column to different fields/properties of subclasses in Hibernate when using table per hierarchy

I have a base (abstract) class A and two subclasses B and C which I want to persist them using table-per-hierarchy approach in Hibernate. 我有一个基类(抽象)A和两个子类B和C,我想在Hibernate中使用每层表的方法来持久化它们。 Suppose B has 'width' and C has 'height' field which are of the same type. 假设B具有“宽度”并且C具有“高度”字段,它们是同一类型。 By default Hibernate creates two separate columns but fills only one column for each row. 默认情况下,Hibernate创建两个独立的列,但每一行仅填充一列。 Now, what happens if I map both fields to the same column in DB, say 'length'? 现在,如果我将两个字段都映射到数据库中的同一列,例如“ length”,会发生什么?

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.INTEGER)
public abstract class A {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer id;
}

@Entity
@DiscriminatorValue("1")
public class B extends A {
    @Column(name="length")
    public Integer width;
}

@Entity
@DiscriminatorValue("2")
public class C extends A {
    @Column(name="length")
    public Integer height;
}

I ran some basic tests and no error happened, but I didn't see anything like this in documentations -considering this practice prevents creation of lots of empty columns in a complex application and should be mentioned - and really don't have the expertise to declare this as a safe practice. 我进行了一些基本测试,但没有发生任何错误,但是我在文档中没有看到类似的东西-考虑到这种做法会阻止在复杂的应用程序中创建许多空列,因此应该予以提及-而且实际上没有专门知识声明这是安全的做法。

Has anyone done something like this? 有人做过这样的事情吗? Are there any drawbacks to this approach? 这种方法有什么缺点吗? Also does the no-NOT-NULL-on-subclasses limitation holds in this scenario? 在这种情况下,子类的no-not-null-null限制也成立吗?

I have not encountered any problems after using this method for two years in production. 在使用这种方法两年后,我没有遇到任何问题。

But notice that you cannot set different restrictions on fields because database sees them as one column. 但是请注意,您不能对字段设置不同的限制,因为数据库将它们视为一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在每个层次的Hibernate表继承策略中跨子类重用列吗? - Can I reuse a column across subclasses in a Hibernate table-per-hierarchy inheritance strategy? 映射“按类分类的表”超类,该超类具有子类,该子类映射另一个“按类分类的表”的子类 - map “table per class hierarchy” superclass having subclasses mapping sub classes of another “table per class hierarchy” Hibernate - 同步映射同一列的两个属性 - Hibernate - synchronize two properties mapping the same column 使用相同ID的Hibernate DB对象映射时出错 - Error when Hibernate DB Object mapping using same ID 每个类层次结构继承使用一个表时的hibernate获取策略 - hibernate fetch strategy when using one table per class hierarchy inheritance 同一个表中不同项目的休眠映射 - Hibernate mapping for different items in the same table 使用休眠模式更新映射表中的其他字段 - update additional fields in the mapping table using hibernate 在子类的 Hibernate 中为每个表指定不同的序列 - Specifying distinct sequence per table in Hibernate on subclasses 使用相同的数据透视表对具有子类的多对多进行休眠 - Hibernate many-to-many with subclasses using the same pivot table hibernate中的一对多映射:将两个不同的属性引用到单个列 - one to many mapping in hibernate : Referring two different properties to a single column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM