public int getCount(long user, long project, long type, String simNumber,
String callBarring, String puk, String local) {
Criteria criteria = session.createCriteria(Sim.class);
if (user != 0) {
criteria.add(Restrictions.eq("user.id", user));
}
if (project != 0) {
Restrictions.eq("project.id", project);
}
if (type != 0) {
criteria.add(Restrictions.eq("type.id", type));
}
if (!"".equals(simNumber) && simNumber != null) {
criteria.add(Restrictions.like("simNumber", "%" + simNumber + "%"));
}
if (!"".equals(callBarring) && callBarring != null) {
criteria.add(Restrictions.eq("callBarring", callBarring));
}
if (!"".equals(puk) && puk != null) {
criteria.add(Restrictions.like("puk", "%" + puk + "%"));
}
if (!"".equals(local) && local != null) {
criteria.createCriteria("user").add(Restrictions.eq("local", local));
}
criteria.setProjection(Projections.rowCount());
return (Integer) criteria.uniqueResult();
}
今天发现的:hibernate里的 criteria.add(Restrictions.eq("*", *));
是有选择的,我的Book对象里的user(对象).id type(对象).id 可以,id是long型。
criteria.add(Restrictions.eq("type.id", type));
但是,遇到String类型的local就不行了。只能
criteria.createCriteria("user").add(Restrictions.eq("local", local));