我有一个模型,其中每个具体类都实现了表格。因此,我有一个抽象类,该类具有跨多个表的通用属性。并且我有以下实体。

    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
    public abstract class BaseForm{

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    protected Long id;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id=id;
    }

    @Column(name = "submission_date")
    protected Date submissionDate;

    public Date getSubmissionDate() {
        return submissionDate;
    }
    public void setSubmissionDate(Date submissionDate) {
        this.submissionDate=submissionDate;
    }

   }

@Entity
@Table(name = "form_a")
public class FormA extends BaseForm{

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "formA", fetch = FetchType.LAZY)
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("id")
    protected List<UserForm> userForms = new ArrayList<UserForm>();

}

@Entity
@Table(name = "form_b")
public class FormB extends BaseForm{

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "formB", fetch = FetchType.LAZY)
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @Fetch(FetchMode.SUBSELECT)
    @OrderBy("id")
    protected List<UserForm> userForms = new ArrayList<UserForm>();

}

@Entity
@Table(name = "user_form")
public class UserForm {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(optional = true)
    protected FormA formA;

    @ManyToOne(optional = true)
    protected FormB formB;

    @ManyToOne(optional = true)
    protected User user;


}

但是每当我尝试在多态查询上使用createAlias时,选择所有加入userForms的表单以返回每个表单的用户信息。它引发一个异常。
2016-01-04 12:21:54,158错误[org.hibernate.util.JDBCExceptionReporter]不是唯一的表/别名:'userforms1_'

    DetachedCriteria baseCR = DetachedCriteria.forClass(BaseForm.class);
    baseCR.createAlias("userForms", "userForms");
            );
    baseCR.add(Restrictions.disjunction()
        .add(Restrictions.isNotNull("userForms.formA"))
        .add(Restrictions.isNotNull("userForms.formB"))
    );
    baseCR.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    baseCR.setProjection(Projections.rowCount());
    List results= ht.findByCriteria(baseCR);


这是生成的休眠查询

SELECT COUNT(*) AS y0_
FROM
  (SELECT id,
    submissionDate,
    1 AS clazz_
  FROM form_a
  UNION
  SELECT id,
    submissionDate,
    2 AS clazz_
  FROM form_b
  ) this_
INNER JOIN user_form userforms1_
ON this_.id=userforms1_.formA_id
INNER JOIN user_form userforms1_
ON this_.id=userforms1_.formB_id
WHERE (userforms1_.formA IS NOT NULL
OR userforms1_.formB IS NOT NULL)


不知道会是什么问题。以及如何解决?

最佳答案

您正在使用超类别名访问子类字段,这在概念上是错误的(尽管在某些情况下可行,但显然并非总是如此)。

您应该考虑将userForms移至基类或将查询转换为类似的内容(JPQL,只需将其转换为等效的Criteria)即可:

select bf from BaseForm bf
  where bf.id in (select fa.id from FormA fa join fa.userForms)
     or bf.id in (select fb.id from FormB fb join fb.userForms)

09-27 08:35