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