Redefine Attached Inheritance Foreign Key Name with JPA / Hibernate

I have a CD class with a Media class inheritance:

CD:

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

MASS MEDIA:

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

JPA automatically generates a foreign key name, and I would like to override this name that I want:

03-10 18: 16: 58.174 [main] DEBUG org.hibernate.SQL - change the cd add table restriction FK_ehd468g2cptgh6bq6sxe75xlf foreign key links (id) media (id)

How to do it? I tried:

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

and

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

but that will not work.

Here is the sql table created:

 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; 

I would like to:

 CONSTRAINT `fk_cd_media` FOREIGN KEY (`id`) REFERENCES `media` (`id`) 
+5
source share
1 answer

The second attempt is the way:

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

The problem is that you encountered a Hibernate error: https://hibernate.atlassian.net/browse/HHH-10352

Edit: problem resolved:

Fix version / s: 5.0.10, 5.2.1, 5.1.1

+2
source

Source: https://habr.com/ru/post/1232863/


All Articles