Java jpa中带取数关系的计数查询
这是我的实体:-Java jpa中带取数关系的计数查询,java,hibernate,jpa,sql-server-2012,Java,Hibernate,Jpa,Sql Server 2012,这是我的实体:- public class ArticleType extends BaseEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "
public class ArticleType extends BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "art_typ_index")
private Integer artTypIndex;
//@Basic(optional = false)
@Column(name = "art_typ_code", nullable = false)
private String artTypCode;
@OneToMany(mappedBy = "artArtTypIndex", fetch = FetchType.LAZY)
private Set<Article> articleSet;
public ArticleType()
{
}
public ArticleType(Integer artTypIndex)
{
this.artTypIndex = artTypIndex;
}
public ArticleType(Integer artTypIndex, String artTypCode)
{
this.artTypIndex = artTypIndex;
this.artTypCode = artTypCode;
}
public Integer getArtTypIndex()
{
return artTypIndex;
}
public void setArtTypIndex(Integer artTypIndex)
{
this.artTypIndex = artTypIndex;
}
public String getArtTypCode()
{
return artTypCode;
}
public void setArtTypCode(String artTypCode)
{
this.artTypCode = artTypCode;
}
@XmlTransient
public Set<Article> getArticleSet()
{
return articleSet;
}
public void setArticleSet(Set<Article> articleSet)
{
this.articleSet = articleSet;
}
}
但是这个查询给了我编译错误:-
org.hibernate.QueryException:
查询指定了联接提取,但提取的关联的所有者不在选择列表中
[FromElement{explicit,非集合联接,获取联接,获取非惰性属性,classAlias=article,role=com.alstom.autofie2.entity.ArticleType.articleSet,tableName=tbl_article,tableAlias=articleset1_,origin=tbl_article_typ
e articletyp0_,columns={articletyp0_.art_-typ_索引,className=com.alstom.autofie2.entity.Article}]
我不明白是什么问题?谢谢。问题是您选择的是计数,而不是实体,但您选择的是联合获取 您不需要急切地加载集合中没有获取的实体 尝试使用join 或者这也行 挑选 articleType,计数(articleType.ArtTypeIndex) 从…起 ArticleType ArticleType 加入获取articleType.articleSet项目 其中articleType.artTypCode=
SELECT
count(articleType.artTypIndex)
FROM
ArticleType articleType
join fetch articleType.articleSet article
where articleType.artTypCode = ?