# OneToMany with inherited classes on both sides

**URL:** https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869
**Category:** Hibernate ORM
**Created:** [June 23, 2023, 9:48pm UTC](https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869 "2023-06-23T21:48:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [June 23, 2023, 9:48pm UTC](https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869/1 "2023-06-23T21:48:33Z")

</div>

Hi,

I am having trouble getting this mapping to work.

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](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/6/6497b057f76dfa6fe7ba91a4034a6f18a7c08a88.png)

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.

```auto
@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;
}

```

One of the company subclasses has a OneToMany collection with one of the computer\_system subclasses. I started by annotating my collection this way:

```auto
@Entity ...
public class Customer {
    @OneToMany(mappedBy = "owner", orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    private List<CustomerComputerSystem> computerSystems;
}

```

The trouble is that hibernate expects the owner field to be in the customer\_computer\_system table - instead the owner field is in the base class, computer\_system. So hibernate wants to create an owner field in customer\_computer\_system.

I tried to explicitly describe the join table this way:

```auto
@Entity ...
public class Customer {
    @OneToMany(orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "computer_system", joinColumns = { @JoinColumn(name = "OWNER_ID") }, inverseJoinColumns = { @JoinColumn(name = "id") })
    private List<CustomerComputerSystem> computerSystems;
}

```

With that change, the OneToMany collection succeeded, but now the schema tool wants to create a foreign key constraint from computer\_system to customer\_computer\_system. That would be incorrect, as not all computer\_systems are customer\_computer\_systems.

```auto
alter table computer_system add constraint FKqwc8n7tsusito0tmeeos68k5c foreign key (id) references customer_computer_system (id);

```

In order to not create that bad FK constraint, I tried expanding my @JoinTable annotations with `foreignKey = @ForeignKey(name = "none")`. Unfortunately that didn’t help.

Any ideas?

(version 6.3.0-SNAPSHOT)

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [June 26, 2023, 8:55am UTC](https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869/2 "2023-06-26T08:55:26Z")

</div>

This mapping looks ok to me

```auto
@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")
    protected Company owner;
}

@Entity
public class Customer {
    @OneToMany(mappedBy = "owner", orphanRemoval = true, cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    private List<CustomerComputerSystem> computerSystems;
}

```

> The trouble is that hibernate expects the owner field to be in the customer\_computer\_system table - instead the owner field is in the base class, computer\_system. So hibernate wants to create an owner field in customer\_computer\_system.

What do you mean by “wants to create an owner field”?

Forget about the join table stuff, that’s not appropriate for this mapping.

---

<div class="post-metadata">

### Author: ![robscala](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/robscala/32/2167_2.png) [@robscala](https://discourse.hibernate.org/u/robscala)
#### Post date: [June 26, 2023, 1:43pm UTC](https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869/3 "2023-06-26T13:43:44Z")

</div>

By “wants to create an owner field” I mean that the schema migrator tool produces this script:

```auto
alter table customer_computer_system add column OWNER_ID bigint;
alter table distributor_computer_system add column OWNER_ID bigint;

```

I’m happy to discover that using the simple mapping works, even though the schema tool complains.

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [June 26, 2023, 2:23pm UTC](https://discourse.hibernate.org/t/onetomany-with-inherited-classes-on-both-sides/7869/4 "2023-06-26T14:23:14Z")

</div>

Ok that looks like a bug 🙂  
Please create an issue in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)) with a test case([hibernate-test-case-templates/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)) that reproduces the issue.
