Hibernate Criteria:在 Subquery/DetachedCriteria 中执行 JOIN

Posted

技术标签:

【中文标题】Hibernate Criteria:在 Subquery/DetachedCriteria 中执行 JOIN【英文标题】:Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria 【发布时间】:2011-02-05 20:18:27 【问题描述】:

我在使用 DetachedCriteria 将 JOIN 添加到子查询时遇到了问题。代码大致如下:

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,但确实包含引发错误的别名

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

【问题讨论】:

能把ProductLineModels的定义和映射贴出来吗? 【参考方案1】:

您的代码在以下行中是否有错字:

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

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

【讨论】:

【参考方案2】:

如果您需要在子查询中连接表,您可以尝试这样做。它在您的分离条件中明确指定 joinType。

subCriteria.createAlias("model.modelLanguages", "modelLang", CriteriaSpecification.LEFT_JOIN);
subCriteria.createAlias("modelLang.language", "lang", CriteriaSpecification.LEFT_JOIN);

【讨论】:

以上是关于Hibernate Criteria:在 Subquery/DetachedCriteria 中执行 JOIN的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate中Criteria的完整用法

分享知识-快乐自己:Hibernate 中Criteria Query查询详解

基于Struts2+Hibernate的Criteria多条件查询

Hibernate中Criteria的完整用法

Hibernate中 Restrictions.like() 方法

在 Hibernate 中结合 NamedQuery 和 Criteria