Hibernate Criteria with group by get error

时间:2014-09-02 10:26:50

标签: java hibernate

我有两个引用表的实体java类(jos_content& jos_article_section)。

@Entity
@Table(name="jos_content", 
uniqueConstraints={@UniqueConstraint(columnNames="id")})
public class ContentEntity implements Serializable {
private int id; 
private Set<ArticleSectionEntity> josArticleSection;    
private String sefUrl;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", unique = true, nullable = false)
public int getId() {
    return id;
}   
public void setId(int id) {
    this.id = id;
}

@Column(name="sef_url")
public String getSefUrl() {
    return sefUrl;
}
public void setSefUrl(String sefUrl) {
    this.sefUrl = sefUrl;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="article_id")
public Set<ArticleSectionEntity> getJosArticleSection() {
    return josArticleSection;
}
public void setJosArticleSection(Set<ArticleSectionEntity> josArticleSection) {
    this.josArticleSection = josArticleSection;
}
}


@Entity
@Table(name="jos_article_section", uniqueConstraints=    {@UniqueConstraint(columnNames="id")})
public class ArticleSectionEntity implements Serializable {
private int id; 
private int articleId;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", nullable=false, length=10)
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

@Column(name="article_id", nullable=false)
public int getArticleId() {
    return articleId;
}
}

我正在使用Criteria接口和group by:

Criteria contentCriteria = session.createCriteria(ContentEntity.class, "content");
// Join two tables (jos_content, jos_article_section) on  jos_content.id =     jos_acticle_section.article_id
contentCriteria = contentCriteria.setFetchMode("josArticleSection", FetchMode.JOIN);
contentCriteria = contentCriteria.add(Restrictions.eq("content.id", storyId));
contentCriteria = contentCriteria.add(Restrictions.eq("content.state",1));          

//Start Group By Clause
PropertyProjection propProjection = Projections.groupProperty("content.id");
contentCriteria = contentCriteria.setProjection(propProjection);
//End Group By

当我运行我的应用程序时,我得到的输出错误为:

contentCriteria >>>> CriteriaImpl(com.itgd.entity.ContentEntity:content[][content.id=374821, content.state=1]content.id)
14:24:39,649 INFO  [STDOUT] Hibernate: select this_.id as y0_ from jos_content this_  where this_.id=? and this_.state=? group by this_.id
14:24:39,659 ERROR [STDERR] java.lang.ClassCastException: java.lang.Integer cannot be cast to com.itgd.entity.ContentEntity

如何解决此错误,是否可以使用Criteria界面将所有字段保留为group by? 如果有人指导我,请告诉我。

1 个答案:

答案 0 :(得分:1)

我们可以使用投影来缩小所选列的列表,但是我们必须决定

  • 我们会使用表示为object[]
  • 的结果
  • 我们会将结果转换为DTO(如果可能的话,转换为原始实体)

如果我们要进行转换,我们必须使用列名帮助变换器。通过传递alias

完成
PropertyProjection propProjection = Projections
   .groupProperty("content.id")
       .as("id") // the alias
   ;

contentCriteria
    .setProjection(propProjection)
    .setResultTransformer(Transformers.aliasToBean(ContentEntity.clas‌​‌​s));

List<ContentEntity> groupedEntities = contentCriteria.list();

然后结果将是ContentEntity的列表,该列表仅填充 id 。参见:

  

alias()as()方法只是将投影实例包装在另一个别名的Projection实例中。

或者我们可以将结果视为object[]

Object[] results = contentCriteria.list();

EXTEND:

如果我们想获得路线实体列表,我们可以将当前查询转换为DetachedCriteria

DetachedCriteria grouped = DetachedCriteria.forClass(ContentEntity.class, "grouped")
    // Filter the Subquery
    .add(...
    // SELECT The User Id  
   .setProjection(propProjection)

所以,上面只返回我们喜欢的ID,主要查询将被他们过滤,同时返回完整的对象(无需转换)

Criteria query = session.createCriteria(ContentEntity.class, "content")
    .add( Subqueries.propertyIn("content.id", grouped) );

点击此处查看类似内容:Hibernate Criteria for "in subselect"