2015-10-03 109 views
5

我有一個類CD與類媒體繼承:覆蓋加盟繼承外鍵與JPA名稱/休眠

CD:

@Entity 
public class CD extends Media { 
    ... 
} 

媒體:

@Entity(name = "media") 
@Inheritance(strategy = InheritanceType.JOINED) 
public abstract class Media extends PersistenceId<Long> { 
    ... 
} 

JPA自動生成一個外鍵名稱,我想重寫這個與我想要的名稱:

03-10 18:16:58.174 [主] DEBUG org.hibernate.SQL - 改變表CD添加 約束FK_ehd468g2cptgh6bq6sxe75xlf外鍵(ID)引用 媒體(ID)

如何做到這一點?我想:

@Entity 
@AssociationOverride(
    name = "id", 
    foreignKey = @ForeignKey(name = "fk_cd_media") 
) 
public class CD extends Media { 
    ... 
} 

@Entity 
@PrimaryKeyJoinColumn([email protected](name = "fk_cd_media")) 
public class CD extends Media { 
    ... 
} 

,但它不工作。

這裏生成創建表的SQL:

CREATE TABLE `cd` (
    `artist` varchar(255) DEFAULT NULL, 
    `year` int(11) NOT NULL, 
    `id` bigint(20) NOT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `FK_ehd468g2cptgh6bq6sxe75xlf` FOREIGN KEY (`id`) REFERENCES `media` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 

我想:

CONSTRAINT `fk_cd_media` FOREIGN KEY (`id`) REFERENCES `media` (`id`) 

回答