0

我正在尝试从休眠条件进行查询,但生成的查询包含不必要的连接。

我的实体映射是这样的:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="A")
public class A {
    @Id 
    private Long id;

    private String champ1;
    private String champ2;
    private String champ3;
}

@Entity
@Table(name="B")
@PrimaryKeyJoinColumn(name="ID")
public class B extends A {}

@Entity
@Table(name="C")
@PrimaryKeyJoinColumn(name="ID")
public class C extends A {}

我想执行这样的查询:

SELECT ID, CHAMP1, CHAMP2 FROM A WHERE CHAMP3 = ?

我的标准是:

Criteria criteria = (Session)em.getDelegate()).createCriteria(A.class);

ProjectionList projections = Projections.projectionList();
projections.add(Projections.property("id"), "id");
projections.add(Projections.property("champ1"), "champ1");
projections.add(Projections.property("champ2"), "champ2");

criteria.setProjection(projections);
criteria.add(Restrictions.eq("champ3", valeur));

(List<A>) criteria.list();

生成的请求是:

SELECT
    a.id,
    a.CHAMP1,
    a.CHAMP2
FROM
    A a 
LEFT OUTER JOIN
    B b 
on a.ID=b.ID 
LEFT OUTER JOIN
    C c 
on a.ID=c.ID 
WHERE
    a.champ3 = ?

我该怎么做才能在我的请求中没有左外连接?谢谢!

4

1 回答 1

0

添加@org.hibernate.annotations.Entity(polymorphisms = PolymorphismType.EXPLICIT)到您的父类,或添加Restrictions.eq("class", A.class)到标准。

于 2013-04-05T14:02:50.963 回答