0

我嘗試用Hibernate 5.2實現單一表繼承。 基類Hibernate繼承與SingleTable

@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; 

} 

作者類擴展PropertyOwner:

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

作曲類擴展PropertyOwner:

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

Person類:

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

物業CLA SS

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

我期待的表結構如下:

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; 

但遺憾的是作者和作曲班,下面的語句

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表,Author,Composer類信息應該保存在propertyOwner表中。

注意:我嘗試了Enumarated註解,但是這次在Property類中我無法定義Setauthor字段,我必須定義Set並將枚舉信息添加到該對象。 在此先感謝。

+0

好像你在映射中缺少'mappedBy'。使用'@OneToMany(mappedBy =「property」)''和'@OneToMany(mappedBy =「person」)' –

回答

1

在您的屬性實體中有多個子類(作曲家和作者)的關聯沒有意義。當你有單表繼承時,即你的財產所有者表將擁有一個property_id的外鍵。 所以相反,你可以有單一的關聯。

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

而且您可以使用DiscriminatorColumn類型將此集合拆分爲作者和作曲者。

+0

由於性能問題,我想在Property和數據庫中使用Author和Composer對象。如果我更改PropertyOwner,我不能使用Author作爲對象。 –

+0

由於您使用單表繼承,因此您將獲得屬性實體中的作者或作曲家對象。 Hibernate會根據discriminmator的值將它轉換爲適當的子類對象。 – shubham