简体   繁体   中英

Hibernate omit table when extended class contains only embeded collection

I am using at the moment the inheritence strategy InheritanceType.JOINED. My extended class contains just a Map of type String which is stored in its own table. I wonder if there is a possibility to omit the table for my extended class which contains just the id.

Please let me know how you would model it differently if there exist no direct option to "omit" the table.

The example shows that I have a base class "Attribute" which get extended by a class "StringMapAttribute". This extendend class just contains a collection. The table for "StringMapAttribute" contains in the end just the "id" which is "Design Overhead" so I can use inheritace and model different attribute types. Therefore it is not ideal to have just a table with one single id column.

// 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>();
}

Thank you for any helpful hints.

Yes the comment from "DuncanKinnear"is correct so I switched to SINGLE_TABLE strategy.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM