My suggestion is to split the base class between your two products so that you can refer to them one at a time. In JPA / Hibernate, this means creating a common table that will contain the common elements of your product, namely a link to CustomerA.
You can accomplish this with the JOINED inheritance strategy:
@Inheritance(strategy=InheritanceType.JOINED)
The disadvantage of using this strategy is that it may run into your existing TABLE_PER_CLASS category already used in the AbstractProduct base class .... although it may be the appropriate base class to use for this. From what I remember, you cannot mix inheritance strategies.
Here is an example:
@Entity @Table(name="CustomerA") public class CustomerA { @Id @GeneratedValue.... @Column(name="a_id") private Long aId @OneToMany(mappedBy="customerA", cascade=CascadeType.ALL) private Set<AbstractProduct> product; } @Entity @Table(name="ProductB") public class ProductB extends AbstractProduct { private String specificProductBValue; } @Entity @Table(name="ProductC") public class ProductC extends AbstractProduct { private String specificProductCValue; } @Entity @Inheritance(strategy=InheritanceType.JOINED) public abstract class AbstractProduct { @Id @GeneratedValue.... private Long id; private String name; @ManyToOne() JoinColumn(name="customer_id") private CustomerA customerA; }
It looks like you're almost on your own example, you just need to use the right inheritance strategy.
In addition, this assumes that you do not have other products that extend the AbstractProduct base class. If you do this and you donβt need the ones your client refers to, you will have to rethink your domain design.
Hope this helps.
source share