问题
Stack
I'm using JPA2.0 with Eclipselink 2.6.3 and Spring 4(JTA).
Problem
I'm trying to override the class A attribute with @OneToMany mapping in class B and class C (which is similar to class B ) so that I can change the target entity to class X and Z respectively while fetching results for class B.
Class A has SINGLE_TABLE inheritance and class B & C are discriminator classes which is maintained by @classExtractor depending on some field value in class A.
Class Z has TABLE_PER_CLASS inheritance and class X & Y extends class Z with same table (just a hack for inheritance to avoid DTYPE).
Expected Result When I query on class B or Class C and fetch getZList() then I should be able to see class X and Y type objects as mentioned in targetEntity in @OneToMany mapping in class B & C
I tried overriding the getter of class A in subclasses like below but the list is always empty.
Class A
@Entity
@Table(name="TABLE_A")
@Customizer(ACustomizer.class)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@ClassExtractor(AExtractor.class)
@InstantiationCopyPolicy
@Cacheable
@Cache( alwaysRefresh=true,
refreshOnlyIfNewer=true,
expiry=300000,
coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES)
public class A implements Serializable {
@Column(name="ABC")
private String abc;
@Transient
private List zlist= new ArrayList<>();
}
Class B
@Entity
public class B extends A{
//problem is here...this mappping doesn't populate super class object
@Access(AccessType.PROPERTY)
@OneToMany(cascade=CascadeType.ALL, mappedBy="class A", targetEntity=Z.class)
@PrivateOwned
public List getZList() {
return super.getZList();
}
}
Class Z
@Entity
@Table(name="TABLE_Z")
@Customizer(ZCustomizer.class)
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Z{
//some fields
//and @oneToOne mapped with class A
}
class X
@Table(name="TABLE_Z")
@Entity
public class X extends Z{
//some fields
}
来源:https://stackoverflow.com/questions/44068524/how-to-apply-onetomany-on-super-class-getter-method-in-multiple-subclasses-with