How to apply @OneToMany on super class getter method in multiple subclasses with different target entity

半城伤御伤魂 提交于 2019-12-25 14:40:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!