Workaround for Hibernate bug HHH-18470

Good afternoon everyone.
Following the Hibernate bug HHH-18470 we are looking for a workaround to override the name of foreign keys for the subclasses to prevent having duplicated foreign key exception.

Here is a sample code of the situation that we have:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@NoArgsConstructor
@Getter
@Setter
public abstract class Person extends BaseEntity {

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    private Family family;
}
@Entity
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Employee extends Person {
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AssociationOverride(name = "family", foreignKey = @ForeignKey(name = "custom_fk_name"))
public class Manager extends Employee {

    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
    private List<Territory> territories = new ArrayList<>();
...
}
@Entity
public class Family extends BaseEntity {
    @Column(nullable = false, columnDefinition = "TEXT")
    private String familyName = "";
}

I expected to be able to override the name of the foriegn key for the subclass Manager but I get the following exception and can’t even run the app.

Property 'family' is inherited from entity 'com.hannoverre.reflex.rke.webapp.module.workspace.application.service.sample.Employee' and may not be overridden using '@AssociationOverride' in entity subclass 'com.hannoverre.reflex.rke.webapp.module.workspace.application.service.sample.Manager'

Could you please let me know why it’s not working and if I can find a better workaround?(changing the structure of the entities or the inheritance strategy is unfortunately not an option for me)

Hibernate version :6.5.2

It appears there is no workaround, but you should maybe not rely on hbm2ddl in the first place. Use a proper schema management tool like Liquibase or Flyway.

@beikov Thanks for your response. Do you have any idea why I get an exception when I try to use @AssociationOverride in the sample cod above?

Sorry, no idea, but someone will certainly look into this as part of HHH-18470

Have you found a workaround for your problem? I’m interested in finding a solution since I’m facing the same bug in my code. I am not able to rename the foreign key constraint in an inherited field.
Thanks in advance

Hi Martoal,

I could not find any workaround at that time except for offering a fix.
You can see the related bug ticket and the fix versions HERE.

I hope it can help you.

Thanks Paria for your quick reply. I have upgraded the project to SpringBoot 3.4.1 (so the hibernate version has also been upgraded to hibernate 6.6.4) and the error is no longer reproduced. Now the constraints for any child entity are generated with an unique identifier.

1 Like