List of usage examples for org.hibernate Criteria createCriteria
public Criteria createCriteria(String associationPath, String alias) throws HibernateException;
From source file:com.floreantpos.report.service.ReportService.java
License:Open Source License
public MenuUsageReport getMenuUsageReport(Date fromDate, Date toDate) { GenericDAO dao = new GenericDAO(); MenuUsageReport report = new MenuUsageReport(); Session session = null;/* w w w . ja v a 2 s . co m*/ try { session = dao.getSession(); Criteria criteria = session.createCriteria(MenuCategory.class); List<MenuCategory> categories = criteria.list(); MenuCategory miscCategory = new MenuCategory(); miscCategory.setName(com.floreantpos.POSConstants.MISC_BUTTON_TEXT); categories.add(miscCategory); for (MenuCategory category : categories) { criteria = session.createCriteria(TicketItem.class, "item"); //$NON-NLS-1$ criteria.createCriteria("ticket", "t"); //$NON-NLS-1$ //$NON-NLS-2$ ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.sum(TicketItem.PROP_ITEM_COUNT)); projectionList.add(Projections.sum(TicketItem.PROP_SUBTOTAL_AMOUNT)); projectionList.add(Projections.sum(TicketItem.PROP_DISCOUNT_AMOUNT)); criteria.setProjection(projectionList); criteria.add(Restrictions.eq("item." + TicketItem.PROP_CATEGORY_NAME, category.getName())); //$NON-NLS-1$ criteria.add(Restrictions.ge("t." + Ticket.PROP_CREATE_DATE, fromDate)); //$NON-NLS-1$ criteria.add(Restrictions.le("t." + Ticket.PROP_CREATE_DATE, toDate)); //$NON-NLS-1$ criteria.add(Restrictions.eq("t." + Ticket.PROP_PAID, Boolean.TRUE)); //$NON-NLS-1$ List datas = criteria.list(); if (datas.size() > 0) { Object[] objects = (Object[]) datas.get(0); MenuUsageReportData data = new MenuUsageReportData(); data.setCategoryName(category.getName()); if (objects.length > 0 && objects[0] != null) data.setCount(((Number) objects[0]).intValue()); if (objects.length > 1 && objects[1] != null) data.setGrossSales(((Number) objects[1]).doubleValue()); if (objects.length > 2 && objects[2] != null) data.setDiscount(((Number) objects[2]).doubleValue()); data.calculate(); report.addReportData(data); } } return report; } finally { if (session != null) { session.close(); } } }
From source file:com.floreantpos.report.service.ReportService.java
License:Open Source License
public ServerProductivityReport getServerProductivityReport(Date fromDate, Date toDate) { GenericDAO dao = new GenericDAO(); ServerProductivityReport report = new ServerProductivityReport(); Session session = null;/* www . j ava 2 s . c o m*/ try { session = dao.getSession(); Criteria criteria = session.createCriteria(User.class); //criteria.add(Restrictions.eq(User.PROP_USER_TYPE, User.USER_TYPE_SERVER)); List<User> servers = criteria.list(); criteria = session.createCriteria(MenuCategory.class); List<MenuCategory> categories = criteria.list(); MenuCategory miscCategory = new MenuCategory(); miscCategory.setName(com.floreantpos.POSConstants.MISC_BUTTON_TEXT); categories.add(miscCategory); for (User server : servers) { ServerProductivityReportData data = new ServerProductivityReportData(); data.setServerName(server.getUserId() + "/" + server.toString()); //$NON-NLS-1$ criteria = session.createCriteria(Ticket.class); criteria.add(Restrictions.eq(Ticket.PROP_OWNER, server)); criteria.add(Restrictions.eq(Ticket.PROP_PAID, Boolean.TRUE)); criteria.add(Restrictions.eq(Ticket.PROP_VOIDED, Boolean.FALSE)); //$NON-NLS-1$ criteria.add(Restrictions.eq(Ticket.PROP_REFUNDED, Boolean.FALSE)); //$NON-NLS-1$ criteria.add(Restrictions.ge(Ticket.PROP_CREATE_DATE, fromDate)); criteria.add(Restrictions.le(Ticket.PROP_CREATE_DATE, toDate)); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.rowCount()); projectionList.add(Projections.sum(Ticket.PROP_NUMBER_OF_GUESTS)); projectionList.add(Projections.sum(TicketItem.PROP_TOTAL_AMOUNT)); criteria.setProjection(projectionList); Object[] o = (Object[]) criteria.uniqueResult(); int totalCheckCount = 0; double totalServerSale = 0; if (o != null) { if (o.length > 0 && o[0] != null) { int i = ((Number) o[0]).intValue(); data.setTotalCheckCount(totalCheckCount = i); } if (o.length > 1 && o[1] != null) { int i = ((Number) o[1]).intValue(); data.setTotalGuestCount(i); } if (o.length > 2 && o[2] != null) { totalServerSale = ((Number) o[2]).doubleValue(); data.setTotalSales(totalServerSale); } } data.calculate(); report.addReportData(data); for (MenuCategory category : categories) { data = new ServerProductivityReportData(); data.setServerName(server.getUserId() + "/" + server.toString()); //$NON-NLS-1$ criteria = session.createCriteria(TicketItem.class, "item"); //$NON-NLS-1$ criteria.createCriteria(TicketItem.PROP_TICKET, "t"); //$NON-NLS-1$ projectionList = Projections.projectionList(); criteria.setProjection(projectionList); projectionList.add(Projections.sum(TicketItem.PROP_ITEM_COUNT)); projectionList.add(Projections.sum(TicketItem.PROP_SUBTOTAL_AMOUNT)); projectionList.add(Projections.sum("t." + Ticket.PROP_DISCOUNT_AMOUNT)); //$NON-NLS-1$ projectionList.add(Projections.rowCount()); criteria.add(Restrictions.eq("item." + TicketItem.PROP_CATEGORY_NAME, category.getName())); //$NON-NLS-1$ criteria.add(Restrictions.ge("t." + Ticket.PROP_CREATE_DATE, fromDate)); //$NON-NLS-1$ criteria.add(Restrictions.le("t." + Ticket.PROP_CREATE_DATE, toDate)); //$NON-NLS-1$ criteria.add(Restrictions.eq("t." + Ticket.PROP_OWNER, server)); //$NON-NLS-1$ criteria.add(Restrictions.eq("t." + Ticket.PROP_PAID, Boolean.TRUE)); //$NON-NLS-1$ criteria.add(Restrictions.eq("t." + Ticket.PROP_VOIDED, Boolean.FALSE)); //$NON-NLS-1$ criteria.add(Restrictions.eq("t." + Ticket.PROP_REFUNDED, Boolean.FALSE)); //$NON-NLS-1$ List datas = criteria.list(); if (datas.size() > 0) { Object[] objects = (Object[]) datas.get(0); data.setCategoryName(category.getName()); data.setTotalCheckCount(totalCheckCount); if (objects.length > 0 && objects[0] != null) { int i = ((Number) objects[0]).intValue(); data.setCheckCount(i); } if (objects.length > 1 && objects[1] != null) { double d = ((Number) objects[1]).doubleValue(); data.setGrossSales(d); } if (objects.length > 2 && objects[2] != null) { double d = ((Number) objects[2]).doubleValue(); if (d > 0) data.setSalesDiscount(d); } data.setAllocation((data.getGrossSales() / totalServerSale) * 100.0); data.calculate(); report.addReportData(data); } } } return report; } finally { if (session != null) { session.close(); } } }
From source file:com.inkubator.hrm.dao.impl.EmpDataDaoImpl.java
@Override public List<EmpData> getAllDataByParamWithDetail(List<Department> deptId, List<GolonganJabatan> golJabId, String[] empTypeName, List<Integer> listAge, List<Integer> listJoinDate, List<String> listNik, int firstResult, int maxResults, Order order) { List<Long> listDepartment = new ArrayList<Long>(); List<Long> listGolJab = new ArrayList<Long>(); for (Department department : deptId) { listDepartment.add(department.getId()); }/* w w w . jav a 2 s . com*/ for (GolonganJabatan golonganJabatan : golJabId) { listGolJab.add(golonganJabatan.getId()); } final org.hibernate.type.Type[] typeJoinDate = new org.hibernate.type.Type[listJoinDate.size()]; Arrays.fill(typeJoinDate, org.hibernate.type.StandardBasicTypes.INTEGER); final org.hibernate.type.Type[] typeAge = new org.hibernate.type.Type[listAge.size()]; Arrays.fill(typeAge, org.hibernate.type.StandardBasicTypes.INTEGER); final StringBuilder joinDateList = new StringBuilder(); final StringBuilder ageList = new StringBuilder(); for (int i = 0; i < listJoinDate.size(); i++) { if (i > 0) { joinDateList.append(","); } joinDateList.append("?"); } for (int i = 0; i < listAge.size(); i++) { if (i > 0) { ageList.append(","); } ageList.append("?"); } Criteria criteria = getCurrentSession().createCriteria(getEntityClass()); /** * automatically get relations of jabatanByJabatanId, department, * company don't create alias for that entity, or will get error : * duplicate association path */ criteria = this.addJoinRelationsOfCompanyId(criteria, HrmUserInfoUtil.getCompanyId()); criteria.add(Restrictions.not(Restrictions.eq("status", HRMConstant.EMP_TERMINATION))); // criteria.createAlias("bioData", "bioData", JoinType.LEFT_OUTER_JOIN); criteria.createAlias("golonganJabatan", "golonganJabatan", JoinType.LEFT_OUTER_JOIN); criteria.createAlias("employeeType", "employeeType", JoinType.LEFT_OUTER_JOIN); // criteria.createCriteria("bioData", "bio", JoinType.LEFT_OUTER_JOIN); if (!deptId.isEmpty()) { criteria.add(Restrictions.in("department.id", listDepartment)); } if (!golJabId.isEmpty()) { criteria.add(Restrictions.in("golonganJabatan.id", listGolJab)); } if (empTypeName.length != 0) { criteria.add(Restrictions.in("employeeType.name", empTypeName)); } if (listJoinDate.get(0) != 0) { criteria.add(Restrictions .sqlRestriction("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS({alias}.join_date)), '%Y')+0 in (" + joinDateList.toString() + ")", listJoinDate.toArray(), typeJoinDate)); } if (listAge.get(0) != 0) { // criteria.createAlias("bioData", "bioData", JoinType.LEFT_OUTER_JOIN); Criteria criteriaBiodata = criteria.createCriteria("bioData", JoinType.LEFT_OUTER_JOIN); // criteria.createCriteria("bioData", JoinType.LEFT_OUTER_JOIN); criteriaBiodata.add(Restrictions.sqlRestriction( "DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS({alias}.date_of_birth)), '%Y')+0 in (" + ageList.toString() + ")", listAge.toArray(), typeAge)); } if (!listNik.isEmpty()) { criteria.add(Restrictions.in("nik", listNik)); } criteria.addOrder(order); criteria.setFirstResult(firstResult); criteria.setMaxResults(maxResults); return criteria.list(); }
From source file:com.inkubator.hrm.dao.impl.EmpDataDaoImpl.java
@Override public Long getTotalByParamWithDetail(List<Department> deptId, List<GolonganJabatan> golJabId, String[] empTypeName, List<Integer> listAge, List<Integer> listJoinDate, List<String> listNik) { List<Long> listDepartment = new ArrayList<Long>(); List<Long> listGolJab = new ArrayList<Long>(); for (Department department : deptId) { listDepartment.add(department.getId()); }// www. j a v a 2s. c om for (GolonganJabatan golonganJabatan : golJabId) { listGolJab.add(golonganJabatan.getId()); } final org.hibernate.type.Type[] typeJoinDate = new org.hibernate.type.Type[listJoinDate.size()]; Arrays.fill(typeJoinDate, org.hibernate.type.StandardBasicTypes.INTEGER); final org.hibernate.type.Type[] typeAge = new org.hibernate.type.Type[listAge.size()]; Arrays.fill(typeAge, org.hibernate.type.StandardBasicTypes.INTEGER); final StringBuilder joinDateList = new StringBuilder(); final StringBuilder ageList = new StringBuilder(); for (int i = 0; i < listJoinDate.size(); i++) { if (i > 0) { joinDateList.append(","); } joinDateList.append("?"); } for (int i = 0; i < listAge.size(); i++) { if (i > 0) { ageList.append(","); } ageList.append("?"); } Criteria criteria = getCurrentSession().createCriteria(getEntityClass()); /** * automatically get relations of jabatanByJabatanId, department, * company don't create alias for that entity, or will get error : * duplicate association path */ criteria = this.addJoinRelationsOfCompanyId(criteria, HrmUserInfoUtil.getCompanyId()); criteria.add(Restrictions.not(Restrictions.eq("status", HRMConstant.EMP_TERMINATION))); // criteria.createAlias("bioData", "bioData", JoinType.LEFT_OUTER_JOIN); criteria.createAlias("golonganJabatan", "golonganJabatan", JoinType.LEFT_OUTER_JOIN); criteria.createAlias("employeeType", "employeeType", JoinType.LEFT_OUTER_JOIN); // criteria.createCriteria("bioData", "bio", JoinType.LEFT_OUTER_JOIN); if (!deptId.isEmpty()) { criteria.add(Restrictions.in("department.id", listDepartment)); } if (!golJabId.isEmpty()) { criteria.add(Restrictions.in("golonganJabatan.id", listGolJab)); } if (empTypeName.length != 0) { criteria.add(Restrictions.in("employeeType.name", empTypeName)); } if (listJoinDate.get(0) != 0) { criteria.add(Restrictions .sqlRestriction("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS({alias}.join_date)), '%Y')+0 in (" + joinDateList.toString() + ")", listJoinDate.toArray(), typeJoinDate)); } if (listAge.get(0) != 0) { Criteria criteriaBiodata = criteria.createCriteria("bioData", JoinType.LEFT_OUTER_JOIN); // criteria.createCriteria("bioData", JoinType.LEFT_OUTER_JOIN); criteriaBiodata.add(Restrictions.sqlRestriction( "DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS({alias}.date_of_birth)), '%Y')+0 in (" + ageList.toString() + ")", listAge.toArray(), typeAge)); } if (!listNik.isEmpty()) { criteria.add(Restrictions.in("nik", listNik)); } return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult(); }
From source file:com.opiframe.DAO.TeacherDAO.java
public static List<Object[]> getTeacherCourses(String name) { List<Object[]> lst = null; try {//from w w w . j a va 2 s. co m ProjectionList projList = Projections.projectionList(); Session session = HibernateUtil.getSessionFactory().openSession(); Criteria criteria = session.createCriteria(Teachers.class, "teach"); criteria.createCriteria("teach.courseses", "courseses"); projList.add(Projections.property("courseses.courseName")); projList.add(Projections.property("courseses.courseId")); criteria.setProjection(projList); criteria.add(Restrictions.eq("teach.teacherName", name)); lst = criteria.list(); session.close(); } catch (Exception e) { e.printStackTrace(); } return lst; }
From source file:com.opiframe.DAO.TeacherDAO.java
public static List<CourseStudents> getStudentsByCourseId(String id) { List<CourseStudents> cs = null; try {/*from w ww . j ava2 s. c o m*/ Session session = HibernateUtil.getSessionFactory().openSession(); Criteria criteria = session.createCriteria(CourseStudents.class, "cs"); criteria.createCriteria("cs.students", "students"); criteria.createCriteria("cs.courses", "courses"); criteria.add(Restrictions.eq("courses.courseId", Integer.parseInt(id))); cs = criteria.list(); for (CourseStudents data : cs) { Hibernate.initialize(data.getCourses().getCourseStudentses()); } session.close(); } catch (Exception e) { e.printStackTrace(); } return cs; }
From source file:com.painiu.core.dao.hibernate.PhotoDAOHibernate.java
License:Open Source License
static Criteria buildPhotoCriteria(final Session session, User user, String[] tags, boolean taggedAll, String text, Relation relation, boolean count) { Criteria criteria = session.createCriteria(Photo.class); if (user != null) { criteria.add(Restrictions.eq("user", user)); if (relation != null) { criteria.add(/* www . j a va 2 s . c o m*/ Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", relation, UserTypes.relation())); } } else { criteria.add( Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", Relation.NONE, UserTypes.relation())); criteria.setFetchMode("user", FetchMode.JOIN); } //if (user == null && group == null) { if (user == null) { Disjunction disjState = Restrictions.disjunction(); disjState.add(Restrictions.eq("state", Photo.State.USER_POPULAR)); disjState.add(Restrictions.eq("state", Photo.State.USER_COMMENDATORY)); disjState.add(Restrictions.eq("state", Photo.State.USER_SENIOR)); criteria.add(disjState); } //if (album != null) { // criteria.createAlias("albumPhotos", "ap"); // criteria.add( Restrictions.eq("ap.album", album) ); //} //if (group != null) { // criteria.createAlias("groupPhotos", "gp"); // criteria.add( Restrictions.eq("gp.group", group) ); //} if ((tags != null && tags.length > 0) || text != null) { Criteria subCriteria = criteria.createCriteria("photoTags", "tags"); if (tags != null && tags.length > 0) { if (taggedAll) { Conjunction conj = Restrictions.conjunction(); for (int i = 0; i < tags.length; i++) { conj.add(Restrictions.eq("tagName", tags[i])); } subCriteria.add(conj); } else { Disjunction disj = Restrictions.disjunction(); for (int i = 0; i < tags.length; i++) { disj.add(Restrictions.eq("tagName", tags[i])); } subCriteria.add(disj); } } if (text != null) { Disjunction disj = Restrictions.disjunction(); disj.add(Restrictions.like("title", text, MatchMode.ANYWHERE)); disj.add(Restrictions.like("description", text, MatchMode.ANYWHERE)); disj.add(Restrictions.eq("tags.tagName", text)); criteria.add(disj); } } // TODO order parameters if (!count) { /*if (album != null) { criteria.addOrder(Order.asc("ap.position")); } else*/ /*if (group != null) { criteria.addOrder(Order.asc("gp.position")); } else {*/ criteria.addOrder(Order.desc("timestamp")); //} } // distinct ? if ((tags != null && tags.length > 1) || text != null) { ProjectionList proj = Projections.projectionList(); proj.add(Projections.property("id")).add(Projections.property("title")) .add(Projections.property("width")).add(Projections.property("height")) .add(Projections.property("address.host")).add(Projections.property("address.dir")) .add(Projections.property("address.filename")).add(Projections.property("address.secret")) .add(Projections.property("address.username")).add(Projections.property("address.fileKey")); if (user == null) { criteria.createAlias("user", "user"); proj.add(Projections.property("user.id")).add(Projections.property("user.username")) .add(Projections.property("user.nickname")).add(Projections.property("user.buddyIcon.host")) .add(Projections.property("user.buddyIcon.dir")) .add(Projections.property("user.buddyIcon.filename")) .add(Projections.property("user.buddyIcon.username")) .add(Projections.property("user.buddyIcon.fileKey")); } criteria.setProjection(Projections.distinct(proj)); criteria.setResultTransformer(new PhotoBeanResultTransformer()); } return criteria; }
From source file:com.webbfontaine.valuewebb.action.pricedb.mp.MPCriteriaContainer.java
License:Open Source License
@Override public Criteria transformUserCriteria(Session session) throws Exception { Criteria criteria = session.createCriteria(MarketPrice.class) .add(Example.create(new MarketPrice()).excludeZeroes()); criteria.createCriteria("sourceOfPrice", "sourceOfPrice"); Junction junction = getMatchAll() ? Restrictions.conjunction() : Restrictions.disjunction(); List<UserCriterion> selectedUserCriteria = getSelectedUserCriteria(); for (UserCriterion userCriterion : selectedUserCriteria) { Criterion transformedCriterion;/* w w w.j a v a 2s . c o m*/ if ("ageOfPrice".equals(userCriterion.getFullKey())) { transformedCriterion = transformAgeOfPriceToPriceDate(userCriterion, getDefinedCriterion("priceDate")); } else { transformedCriterion = userCriterion.transform(); } if (transformedCriterion != null) { criteria.add(junction.add(transformedCriterion)); } } return criteria; }
From source file:com.webbfontaine.valuewebb.action.pricedb.mp.PRDConsult.java
License:Open Source License
private void fillMPs(MarketPrice mp) { Criteria criteria = PersistenceUtil.getSession(entityManager).createCriteria(MarketPrice.class); criteria.createCriteria("sourceOfPrice", "sourceOfPrice"); criteria.setMaxResults(Constants.MAX_RECORDS_IN_FINDER_RESULT).addOrder(Order.desc("priceDate")); if (mp.getHsCode() != null) { criteria.add(Restrictions.like("hsCode", mp.getHsCode(), MatchMode.START)); }//from w w w . java2s . c o m if (mp.getDpd() != null) { criteria.add(Restrictions.ilike("dpd", mp.getDpd(), MatchMode.ANYWHERE)); } if (!StringUtils.isEmpty(mp.getVmRuleCode())) { criteria.add(Restrictions.eq("vmRuleCode", mp.getVmRuleCode())); } mpDB.addAll(criteria.list()); }
From source file:com.webbfontaine.valuewebb.action.pricedb.pdss.PdssCriteriaContainer.java
License:Open Source License
@Override public Criteria transformUserCriteria(Session session) throws Exception { if (fakeToRealPropertyMap == null || entityClass == null) { throw new RuntimeException("Target bean/properties were not initialized"); }//ww w .ja va 2 s .com Criteria criteria = session.createCriteria(entityClass) .add(Example.create(entityClass.newInstance()).excludeZeroes()); List<String> aliases = new ArrayList<>(); Junction junction = getMatchAll() ? Restrictions.conjunction() : Restrictions.disjunction(); List<UserCriterion> selectedUserCriteria = getSelectedUserCriteria(); for (UserCriterion fakeCriterion : selectedUserCriteria) { if (beanHasCriterionProperty(fakeCriterion.getFullKey())) { UserCriterion realCriterion = prepareUCForBean(fakeCriterion); String alias = extractAlias(realCriterion.getFullKey()); if (alias != null) { if (!aliases.contains(alias)) { aliases.add(alias); criteria.createCriteria(alias, alias); } } Criterion transformedCriterion; if ("ageOfPrice".equals(fakeCriterion.getFullKey())) { transformedCriterion = transformAgeOfPriceToPriceDate(fakeCriterion, realCriterion); } else { transformedCriterion = realCriterion.transform(); } criteria.add(junction.add(transformedCriterion)); } } return criteria; }