当扩展 class 仅包含嵌入集合时,Hibernate 省略 table

Hibernate omit table when extended class contains only embeded collection

我目前正在使用继承策略InheritanceType.JOINED。我的扩展 class 只包含一个字符串类型的映射,它存储在它自己的 table 中。我想知道是否有可能为仅包含 ID 的扩展 class 省略 table。

如果 "omit" 和 table 没有直接选项,请告诉我您将如何以不同的方式建模它。

该示例显示我有一个基础 class "Attribute",它由 class "StringMapAttribute" 扩展。这个 extendend class 只包含一个集合。 "StringMapAttribute" 的 table 最后仅包含 "id",即 "Design Overhead",因此我可以使用继承和建模不同的属性类型。因此,只有一个 id 列的 table 是不理想的。

// Base class
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING)
public abstract class Attribute<T> implements Serializable {
  @Id
  @GeneratedValue
  private int id;
  private String name;
  ...
}

// Extended class with embedded map 
@Entity
@Table(name="attribute_string") --> I like not to keep that table
@DiscriminatorValue("STRING")
public class StringMapAttribute extends Attribute<Map<String,String>> {

  @ElementCollection
  @CollectionTable(name= "attribute_string_language",   joinColumns = @JoinColumn(name = "attribute_id")
  )
  @MapKeyColumn(name="language")
  @Column(name = "value")
  private Map<String, String> value = new HashMap<String, String>();
}

感谢您提供任何有用的提示。

是的,"DuncanKinnear" 的评论是正确的,所以我切换到 SINGLE_TABLE 策略。

Yes, but the disadvantage of the JOINED strategy is that you MUST have a separate table for each class, and for your StringMapAttribute class that doesn't make sense as there are no columns in the table (apart from the ID). You need to consider using the SINGLE_TABLE strategy. Modern relational databases can store sparse-column tables (many NULL columns) just as efficiently as a bunch of separate tables, and you don't have the performance hit of having to join the tables together. – DuncanKinnear