继承表的外键名称 [英] Foreign Key name for inherited tables

查看:102
本文介绍了继承表的外键名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含外键的基本实体,并使用TABLE_FOR_CLASS作为继承策略.并且有一些从该类扩展的子类,因此这些子类包含相同的外键引用.

I have a base entity which contains a foreign key and uses TABLE_FOR_CLASS as an inheritance strategy. And there are some subclasses which extends from this class so these subclasses contains the same foreign key reference.

由于我公司的标准,我不允许共享代码.

I am not allowed to share code due to my company's standards.

我想给该外键命名,因为自动生成的外键名称超过30个字符,这会在Oracle 12c中导致错误,并且hibernate无法创建表.

I want to give a name to that foreign key since auto-generated foreign key name exceeds 30 character which causes an error in Oracle 12c and hibernate cannot create the tables.

当我在基类中使用@ForeignKey(name ="FK_XXX")时,子类中的外键名称将变为"FK_XXX9091321asdasdasdas"等.它将生成的名称附加到基类的外键名称中.

When I use @ForeignKey(name="FK_XXX") in base class, the foreign key name in subclasses becomes "FK_XXX9091321asdasdasdas" etc. It appends generated name to base class' foreign key name.

我该如何解决这个问题?就像我暗示的那样,我的实际问题是休眠时会自动生成超过30个字符的外键名称,这在Oracle 12中是不允许的.

How can I solve this problem? As I imply, my actual problem is having more than 30 character foreign key names when hibernates generates automatically which is not allowed in Oracle 12.

推荐答案

由于您的外键将出现在许多表中,并且它的每次出现对于数据库都是唯一的,因此您应该为所有子类覆盖它,因此下面的注释应放在上方每个具有唯一外键名称的子类.我假设BaseClass中指向相关实体的字段称为entityField.

As your foreign key will appear in many tables and its every occurence is unique for database, you should override it for all subclasses, so annotation below should be placed above every subclass with unique foreign key name. I assumed that field in BaseClass which is pointing to related entity is called entityField.

@AssociationOverride(name = "entityField", 
                     foreignKey = @ForeignKey(name="FK_XXX1"))
public class MySubclass extends BaseClass {
    // ...
}

请注意,AssociationOverride.foreignKey是在JPA 2.1中添加的,不会与以前的版本一起编译.

Please note that AssociationOverride.foreignKey was added in JPA 2.1 and won't compile with former versions.

或者,您可以实现Hibernate's Custom Naming Strategy(这可能是您的最佳选择,因为您在其他方面也会遇到类似的问题),有关更多信息,请参见

Alternatively you can implement Hibernate's Custom Naming Strategy (and it would be probably the best option for you as you will have similar problems with other things), for more info see here.

这篇关于继承表的外键名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆