请求没有左外连接的儿童

时间:2013-04-05 13:59:10

标签: hibernate inheritance criteria

我正在尝试从Hibernate标准进行查询,但生成的查询包含不必要的连接。

我的实体映射就是这样:

@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 = ?

如何在我的请求中没有左外连接? 谢谢!

1 个答案:

答案 0 :(得分:0)

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