Hibernate Criteria:在子查询/ DetachedCriteria中执行JOIN [英] Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria

查看:136
本文介绍了Hibernate Criteria:在子查询/ DetachedCriteria中执行JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用DetachedCriteria将JOIN添加到子查询的问题。代码看起来大致如下所示:

I'm running into an issue with adding JOIN's to a subquery using DetachedCriteria. The code looks roughly like this:

Criteria criteria = createCacheableCriteria(ProductLine.class, "productLine");
criteria.add(Expression.eq("productLine.active", "Y"));

DetachedCriteria subCriteria = DetachedCriteria.forClass(Model.class, "model");
subCriteria.setProjection(Projections.rowCount());
subCriteria.createAlias("model.modelLanguages", "modelLang");
subCriteria.createAlias("modelLang.language", "lang");
criteria.add(Expression.eq("lang.langCode", "EN"));
subCriteria.add(Restrictions.eqProperty("model.productLine.productLineId","productLine.productLineId"));
criteria.add(Subqueries.lt(0, subCriteria));

但是,记录的SQL不包含子查询中的JOIN,但确实包含抛出的别名错误

But the logged SQL does not contain the JOIN in the subquery, but does include the alias which is throwing an error

SELECT *
FROM PRODUCT_LINE this_
WHERE this_.ACTIVE=?
AND ?                  <
  (SELECT COUNT(*) AS y0_
  FROM MODEL this0__
  WHERE lang3_.LANG_CODE      ='EN'
  AND this0__.PRODUCT_LINE_ID     =this_.ID
  )

如何将连接添加到DetachedCriteria?

@Entity
@Table(name = "PRODUCT_LINE")
public class ProductLine implements java.io.Serializable {

    private long productLineId;
    private char active;
    private Set<Models> models = new HashSet<Models>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "productLine")
    public Set<Models> getModels() {
        return this.models;
    }

}

@Entity
@Table(name = "MODEL")
public class Model implements java.io.Serializable {

    private long modelId;
    private ProductLine productLine;
    private String name;
    private Set<ModelLanguages> modelLanguages = new HashSet<ModelLanguages>(0);

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_LINE_ID")
    public ProductLine getProductLine() {
        return this.productLine;
    }

    @Column(name = "NAME", nullable = false)
    public String getName() {
        return this.name;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "model")
    public Set<ModelLanguages> getModelLanguages() {
        return this.modelLanguages;
    }

}

@Entity
@Table(name = "MODEL_LANGUAGES")
public class ModelLanguages implements java.io.Serializable {

    private long id;
    private Language language;
    private Model model;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LANGUAGE_ID", nullable = false, insertable = false, updatable = false)
    public Language getLanguage() {
        return this.language;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MODEL_ID", nullable = false, insertable = false, updatable = false)
    public Model getModel() {
        return this.model;
    }
}


@Entity
@Table(name = "LANGUAGES", uniqueConstraints = @UniqueConstraint(columnNames = "LANG_CODE"))
public class Language implements java.io.Serializable {

    private long languageId;
    private String langCode;
    private Set<ModelLanguages> modelLanguages = new HashSet<ModelLanguages>(
            0);

    @Column(name = "LANG_CODE", unique = true, nullable = false)
    public String getLangCode() {
        return this.langCode;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "language")
    public Set<ModelLanguages> getModelLanguages() {
        return this.modelLanguages;
    }
}

Hibernate version: 3.2.6.ga
Hibernate core: 3.3.2.GA
Hibernate annotations: 3.4.0.GA
Hibernate commons-annotations: 3.3.0.ga
Hibernate entitymanager: 3.4.0.GA
Hibernate validator: 3.1.0.GA


推荐答案

不要在代码中输入以下错误:

Don't you have a typo in your code at the following line :

criteria.add(Expression.eq("lang.langCode", "EN"));

我认为,您应该在子标准中添加此限制,而不是标准。

I think, you should add this restriction on the subcriteria and not the criteria.

这篇关于Hibernate Criteria:在子查询/ DetachedCriteria中执行JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆