hibernate表每个具体类@AssociateOverride仍然使用parent属性加入

时间:2016-01-07 18:36:55

标签: java hibernate polymorphism

我为每个具体类实现了hibernate表。我的模型如下,

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@TableGenerator(name="baseform", table="BASEFORM_SEQUENCE", allocationSize=1)
public abstract class BaseForm implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator="baseform")
    @XmlTransient
    protected Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    @JoinColumn(name = "form_id")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("id")
    protected List<CaseNumber> caseNumberList = new ArrayList<CaseNumber>();

    public List<CaseNumber> getCaseNumberList() {
        return caseNumberList;
    }
    public void setCaseNumberList(List<CaseNumber> caseNumberList) {
        this.caseNumberList = caseNumberList;
    }
}

我有以下2个实体扩展BaseForm,

@Entity
@Table(name = "form_a")
@AssociationOverrides({ 
    @AssociationOverride(name = "caseNumberList", joinColumns = @JoinColumn(name = "forma_id"))
})
public class FormA extends BaseForm  {

}

@Entity
@Table(name = "form_b")
@AssociationOverrides({ 
    @AssociationOverride(name = "caseNumberList", joinColumns = @JoinColumn(name = "formb_id"))
})
public class FormB extends BaseForm {

}

每当我尝试查询所有BaseForm并执行getCaseNumbersList()时,它仍使用父joinColumn并且它不会应用AssociateOverride,

    List<BaseForm>  baseFormList = new ArrayList<BaseForm>();
    DetachedCriteria baseCR = DetachedCriteria.forClass(BaseForm.class);
    baseCR.add(Restrictions.like("organizationId", orgId, MatchMode.ANYWHERE));
    baseCR.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    baseFormList = (List<BaseForm>) ht.findByCriteria(baseCR);
    for(BaseForm form : baseFormList){
            if(!form.getCaseNumbersList().isEmpty()){
                     baseFormList.add(form);
            }
      }

如果有人对此问题有任何想法或解决方法,我将不胜感激?

0 个答案:

没有答案