使用 SingleTable 的 Hibernate 继承

Hibernate inheritance with SingleTable

我尝试使用 hibernate 5.2 实现单个 table 继承。 基地 Class

@Entity 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType= DiscriminatorType.STRING)
@DiscriminatorValue("HAKSAHIBI")
@DiscriminatorOptions(force=true)
public class PropertyOwner implements  implements Serializable{
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    long id;
    @ManyToOne 
    Property property;
   @ManyToOne
 Person person; 

}

作者 class 扩展 属性所有者:

@Entity 
@DiscriminatorValue("AUTHOR")
class Author extends PropertyOwner {
}

作曲家 class 扩展 属性 所有者:

@Entity 
@DiscriminatorValue("COMPOSER")
class Composer extends PropertyOwner {
}

人Class:

public class Person {
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    long id;
String title; 
}

属性 class

public class Property{
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    long id;
String title; 

@OneToMany
Set<Composer> composers = new HashSet<Composer>();

@OneToMany
Set<Author> authors = new HashSet<Author>(); 
}

我期望 table 结构如下:

CREATE TABLE `Property` (
  `id` bigint(20) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `Person` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `PropertyOwner` (
  `type` varchar(31) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `property_id` bigint(20) DEFAULT NULL,
  `person_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK77apjkkl14xwe45rs1ocgtt4u` (`property_id`),
  KEY `FKqagfofgupovfly26enivfhm3j` (`person_id`),
  CONSTRAINT `FK77apjkkl14xwe45rs1ocgtt4u` FOREIGN KEY (`property_id`) REFERENCES `property` (`id`),
  CONSTRAINT `FKqagfofgupovfly26enivfhm3j` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

但不幸的是,它使用以下语句

为 Author 和 Composer classes 创建了另一个 table
CREATE TABLE `property_propertyOwner` (
  `Property_id` bigint(20) NOT NULL,
  `author_id` bigint(20) NOT NULL,
  `composer_id` bigint(20) NOT NULL,
  UNIQUE KEY `UK_rv9fxc06rcydqp6dqpqbmrkie` (`author_id`),
  UNIQUE KEY `UK_fm4v55i021l5smuees0vs3qmy` (`composer_id`),
  KEY `FKt3yqcltkd0et8bj08ge0sgrqb` (`Property_id`),
  CONSTRAINT `FK1pj2606cjxrb70ps89v7609hg` FOREIGN KEY (`author_id`) REFERENCES `PropertyOwner` (`id`),
  CONSTRAINT `FKfdh3r95jffvd07u3xn21824r7` FOREIGN KEY (`composer_id`) REFERENCES `PropertyOwner` (`id`),
  CONSTRAINT `FKt3yqcltkd0et8bj08ge0sgrqb` FOREIGN KEY (`Property_id`) REFERENCES `Property` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我做错了什么?我预计不应创建 property_PropertyOwner table 并且 Author 和 Composer classes 信息应保存在 propertyOwner table 中。

注意:我尝试了 Enumarated 注释,但这次在 属性 class 我无法定义 Setauthor 字段,我必须定义 Set 并将枚举信息添加到该对象。 提前致谢 。

在您的 属性 entity.As 中拥有子类(作曲家和作者)的多个关联是没有意义的,您只有一个 table 继承,即您的 属性 所有者table 将有一个外键 property_id。 因此,您可以拥有单一关联。

@OneToMany
Set<PropertyOwner> composers = new HashSet<PropertyOwner>();

并且您可以使用 DiscriminatorColumn 类型在内存中将此集合拆分为作者和作曲家。