I have two base classes, company and computer_system. Each of the base classes has two subclasses, and they use the InheritanceType.JOINED strategy.
!company_computer_systems.png|width=879,height=232!
Each computer system has a company owner. A customer_computer_system is owned by a customer_company and a distributor_computer_system is owned by a distributor_company.
{noformat}@Entity ... @Inheritance(strategy= InheritanceType.JOINED) public abstract class ComputerSystem { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id;
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "OWNER_ID", foreignKey = @ForeignKey()) protected Company owner; } {noformat}
One of the company subclasses has a OneToMany collection with one of the computer_system subclasses.
{noformat}@Entity ... public class CustomerCompany { @OneToMany(mappedBy = "owner", orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) private List<CustomerComputerSystem> computerSystems; } {noformat}
With this mapping, I am able to access the list of computer systems.
The problem is that when I use the schema migrator tool to produce an update script, it creates this:
{noformat}alter table if exists customer_computer_system add column OWNER_ID bigint; alter table if exists distributor_computer_system add column OWNER_ID bigint;{noformat}
It wants to add “id” fields to the ComputerSystem subclasses.
The test case is attached and is also here - [https://github.com/robscala/onetomany_inherited_test_case|https://github.com/robscala/onetomany_inherited_test_case|smart-link]
[^HHH-16860.zip]
One thing go to note. This test case properly shows the problem in Hibernate 6.2.1, but fails to create the EMF in later versions due to an assertion error in the SimpleForeignKeyDescriptor. If I turn off assertions, the test case completes even in version 6.3.0-SNAPSHOT, showing the same incorrect update script. |
|