Reputation: 239
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.
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.
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.
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.
Upvotes: 3
Views: 2168
Reputation: 12122
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 {
// ...
}
Please note that AssociationOverride.foreignKey
was added in JPA 2.1 and won't compile with former versions.
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.
Upvotes: 5