I have these two Entity java class which is referring the tables (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;
}
}
I'm using Criteria interface with group by like this:
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
When I running my application, i'm getting this output with error as:
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
How do I have to resolve this error and is it possible to keep all the fields with group by, using Criteria interface? Please let know, if anyone guide me.