JPA mapping for one to many with inheritance mapping
annotations, hibernate, jakarta-ee, java, jpa
Solution
if you're using hibernate you can add a `@Where` condition to the `@OneToMany` Mappings.
E.g:
@OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true)
@JoinColumn(name = "foreinKeyCol", nullable = false)
@OrderColumn(name = "orderCol")
@Where(clause="someDisc=1")
private List<Child1> child1 = new ArrayList<>();
have a look at the api: http://docs.jboss.org/hibernate/core/4.1/javadocs/org/hibernate/annotations/Where.html
and: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-collection
Problem
I have one scenario. ``` @Entity @Table(name = "someTable") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Access(AccessType.FIELD) @DiscriminatorColumn(name = "someDisc") public abstract class AbstractClass{} ``` and ``` @Entity @Access(AccessType.FIELD) @DiscriminatorValue("1") public class Child1 extends AbstractClass{ } @Entity @Access(AccessType.FIELD) @DiscriminatorValue("2") public class Child2 extends AbstractClass{ } ``` Now in 3rd table I want something like this ``` @Entity @Table public class ThridTable{ @OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true) @JoinColumn(name = "foreinKeyCol", nullable = false) @OrderColumn(name = "orderCol") private List<Child2> child2 = new ArrayList<>(); @OneToMany(cascade = CascadeType.ALL, fetch = EAGER, orphanRemoval = true) @JoinColumn(name = "foreinKeyCol", nullable = false) @OrderColumn(name = "orderCol") private List<Child1> child1 = new ArrayList<>(); //more setters/getters } ``` now while it persists well and values are getting saved correctly in the table. The problem I face while fetching object using ThridTable object. The generated query doesn't ditinguish between two instances ie child1 and child2 in same table and tries to update object of child2 in child one.