私はテーブルPersonを持っていて、 "birthDate"がnullでなく、それらが学生である人をすべて数えたいとします。 は、私が2列持つと仮定:いくつかの基準を持つHibernateのカウント行
birthDate Date (can be null)
isStudent boolean (default: false)
私は休止状態使用して、これをどのように行うことができますが..?
私はテーブルPersonを持っていて、 "birthDate"がnullでなく、それらが学生である人をすべて数えたいとします。 は、私が2列持つと仮定:いくつかの基準を持つHibernateのカウント行
birthDate Date (can be null)
isStudent boolean (default: false)
私は休止状態使用して、これをどのように行うことができますが..?
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.isNotNull("birthDate"));
crit.add(Restrictions.eq("isStudent", true));
crit.setProjection(Projections.rowCount());
Integer count = (Integer)crit.uniqueResult();
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.isNotNull("birthDate"));
crit.add(Restrictions.eq("isStudent", true));
List<Person> students = crit.list();
Integer count = students.size();
場合、または1つだけカウント値を求めており、何のリストが返されない:
Criteria crit = session.createCriteria(Person.class);
crit.setProjection(Projections.rowCount());
crit.add(Restrictions.isNotNull("birthDate"));
crit.add(Restrictions.eq("isStudent", true));
return (Long) crit.uniqueResult();
あなたはそのサイズを知るためにリスト全体を取り出していますか?基本的なHQLクエリで十分な場合にCriteriaを使用するのはなぜですか? –
@JB Nizet私はおそらくリストを他のロジックのためにも使用しているからです。 – NimChimpsky
文字列を連結するよりも基準を優先します.SQLを文字列として書き込む場合は、ちょっと休止状態の目的を打ち負かします。 – NimChimpsky
Number count = (Number) session.createQuery(
"select count(p.id) from Person p"
+ " where p.birthDate is not null and p.isStudent = true").uniqueResult();
それを使用してください。それはあなたが何をしようとしている?
public class IncIncidentListGridModel
{
private String historyStatus = null;
public String getHistoryStatus()
{
return historyStatus;
}
public void setHistoryStatus(String historyStatus)
{
this.historyStatus = historyStatus;
}
public void run()
{
IncIncidentListGridModel resultCount =
(IncIncidentListGridModel) ((SQLQuery) hibersession
.createSQLQuery("select count(ch.incident_capa_history_id) as historyStatus from incident_capa_history ch, incident_capa ic where ic.incident_capa_id = ch.FK_incident_capa_id and ic.incident_capa_id='011'"))
.addScalar("historyStatus", Hibernate.STRING)
.setResultTransformer(
Transformers.aliasToBean(IncIncidentListGridModel.class))
.uniqueResult();
System.out.println("count result" + resultCount.getHistoryStatus());
}
}
回答の一部だけでなく、回答を適切にフォーマットしてください。 – konqi
「使用しています。動作しています」というのは役に立つコメントではありません。なぜ誰かが投稿された回答がうまくいかないと思いますか? –
int result= (int)((long)session.createQuery("select count(p) from User p where p.mobileNumber = :pMobileNumber")
.setParameter("pMobileNumber", mobileNumber).uniqueResult());
を働いていますかこれは基本的なクエリです。あなたはHibernateのドキュメントを読んだことがありますか? http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html –