Java Code Examples for org.hibernate.Session.createCriteria()

The following are Jave code examples for showing how to use createCriteria() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: NewsSystem   File: NewsInfoDAOImpl.java   View Source Code Vote up 8 votes
/**
 * 根据条件查询指定新闻
 */
@Override
public List<?> getNewsInfoByConditionAndPage(NewsInfo condition, int page, int pageSize) {
	Session session = SessionFactory.getCurrentSession();
	Criteria criteria = session.createCriteria(NewsInfo.class);
	if (condition != null) {
		if (condition.getTopic() != null && condition.getTopic().getId() != null ) {
			criteria.add(Restrictions.eq("topic.id", condition.getTopic().getId()));
		}
		if (condition.getTitle() != null && !"".equals(condition.getTitle())) {
			criteria.add(Restrictions.like("title", condition.getTitle(), MatchMode.ANYWHERE));
		}
	}
	criteria.setFirstResult(pageSize * (page - 1));
	criteria.setMaxResults(pageSize);
	criteria.addOrder(Order.desc("createDate"));
	return criteria.list();
}
 
Example 2
Project: sjk   File: CatalogConvertorDaoImpl.java   View Source Code Vote up 6 votes
@Override
public CatalogConvertor getByMarketApp(Session sess, String marketName, short catalog, int subCatalog) {
    Criteria cri = sess.createCriteria(CatalogConvertor.class);
    CatalogConvertor catalogConvertor = new CatalogConvertor();
    catalogConvertor.setMarketName(marketName);
    catalogConvertor.setCatalog(catalog);
    catalogConvertor.setSubCatalog(subCatalog);
    Example example = Example.create(catalogConvertor);
    example.excludeZeroes();
    cri.add(example);

    Object o = cri.uniqueResult();
    if (o != null) {
        catalogConvertor = (CatalogConvertor) o;
    }
    return catalogConvertor;
}
 
Example 3
Project: FlashBoard   File: UserDaoImpl.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Override
public List<String> getAllPromotees(){
	Session session = HibernateUtil.getSession();
	List<String> promotees = new ArrayList<>();
	
	Criteria criteria;
	
	try {
		criteria = session.createCriteria(User.class);
		promotees = criteria.add(Restrictions.ge("avgScore", 70.0))
				.add(Restrictions.like("approved", true))
				.setProjection(Projections.property("username")).list();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}finally {
		session.close();
	}
	
	if(promotees.isEmpty()) {
		promotees = null; 
	}
	
	return promotees;
}
 
Example 4
Project: stuffEngine   File: HibernateDepartmentDao.java   View Source Code Vote up 6 votes
@Override
public List<Department> getDepartmentsList() {
    logger.info("Запрос к базе на чтение списка всех отделов");
    List<Department> dhList;

    Session session = getSession();
    try{
        Criteria crit = session.
                createCriteria(Department.class);
        dhList = crit.list();
    }catch (HibernateException e){
        logger.error(e.getStackTrace());
        throw new MyRuntimeException(StuffExceptions.DATABASE_ERROR);
    }

    return dhList;
}
 
Example 5
Project: sdudoc   File: BookDaoImpl.java   View Source Code Vote up 6 votes
@Override
public Pager<Book> searchByTitle(String title, int pageNo, int pageSize) {
	
	Session session = sessionFactory.getCurrentSession();
	Criteria criteria = session.createCriteria(Book.class);
	criteria.add(Restrictions.like("bookTitle", "%"+title+"%"));
	long recordTotal = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
	criteria.setProjection(null);
//	criteria.addOrder(Order.desc("clickTimes"));
	criteria.setFirstResult((pageNo - 1) * pageSize);
	criteria.setMaxResults(pageSize);
	List<Book> results = criteria.list();
	System.out.println(results.size()+"-------数据多少");
	Pager<Book> page=new Pager<Book>(pageSize, pageNo, recordTotal, results);
	return page;
}
 
Example 6
Project: OSWf-OSWorkflow-fork   File: HibernateLoader.java   View Source Code Vote up 5 votes
@Override
protected InputStream fetchProcessDefinition(WorkflowLocation workflowLocation) throws WorkflowLoaderException {
 
    String processDefinition = null;
    Session session = null;
    Transaction transaction = null;

    if(sessionFactory == null)
        throw new WorkflowLoaderException("'sessionFactory' has not been defined for HibernateLoader");

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        
        Criteria criteria = session.createCriteria(HibernateProcessDescription.class);
        criteria.add(Restrictions.eq("workflowName", workflowLocation.location));
        
        HibernateProcessDescription hpd = (HibernateProcessDescription)criteria.uniqueResult();
        
        if(hpd != null)
            processDefinition = hpd.getContent();

        transaction.commit();

    } catch(HibernateException hibernateException) {
        throw new WorkflowLoaderException(hibernateException);
    } finally {

        if (transaction != null && transaction.isActive())
             transaction.rollback();

        if (session != null) 
            session.close();
    }
    
    if(processDefinition == null)
        throw new WorkflowLoaderException("Process definition '" + workflowLocation.location + "' not found");
    
    return new ByteArrayInputStream(processDefinition.getBytes());
}
 
Example 7
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public List<App> listForDownloads(Session session, int currentPage, int pageSize) {
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.property("id"));
    proList.add(Projections.property("pkname"));
    proList.add(Projections.property("marketName"));

    Criteria cri = session.createCriteria(App.class);
    cri.setProjection(proList);
    cri.setMaxResults(pageSize);
    cri.setFirstResult(HibernateHelper.firstResult(currentPage, pageSize));
    // 先全部更新.
    // cri.add(Restrictions.or(Restrictions.ltProperty("realDownload",
    // "lastDayDownload"),
    // Restrictions.ne("deltaDownload", 0)));
    List<Object[]> list = HibernateHelper.list(cri);
    List<App> apps = null;
    if (list != null) {
        apps = new ArrayList<App>(list.size());
        for (Object[] obj : list) {
            App e = new App();
            e.setId((Integer) obj[0]);
            e.setPkname((String) obj[1]);
            e.setMarketName((String) obj[2]);
            apps.add(e);
        }
        list.clear();
    }
    return apps;
}
 
Example 8
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 5 votes
@Override
public List<Comment> getQuizComments(int quizId) {
	List<Comment> comments = new ArrayList<>();
	Session session = HibernateUtil.getSession();
	Criteria criteria = null;
	Quiz q = null;
	
	Query query = null;
	StudyGuide sg = null;
	String hql = "FROM StudyGuide WHERE studyGuideId = :id";
	
	try {
		criteria = session.createCriteria(Quiz.class);
		q = (Quiz)criteria.add(Restrictions.like("quizId", quizId)).uniqueResult();
		
		comments = q.getComments();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}
	
	if(comments.isEmpty()) {
		return null;
	}
	
	return comments;
}
 
Example 9
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public MarketApp get(Session session, String marketName, String pkname) {
    Criteria cri = session.createCriteria(MarketApp.class);
    cri.add(Restrictions.eq("pkname", pkname));
    cri.add(Restrictions.eq("marketName", marketName));
    cri.setCacheable(false);
    Object o = cri.uniqueResult();
    if (o != null) {
        return (MarketApp) o;
    }
    return null;
}
 
Example 10
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public String getOfficialSigSha1(Session session, String pkname) {
    Projection officialSigSha1 = Projections.property("officialSigSha1");
    Criteria cri = session.createCriteria(App.class);
    cri.setProjection(officialSigSha1);
    cri.add(Restrictions.eq("pkname", pkname));
    cri.add(Restrictions.isNotNull("officialSigSha1"));
    List<String> list = HibernateHelper.list(cri);
    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }
    return null;
}
 
Example 11
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public MarketApp getByManyId(Session sess, String marketName, Integer appIdOfMarket, int apkIdOfMarket) {
    Criteria cri = sess.createCriteria(MarketApp.class);
    if (appIdOfMarket != null && appIdOfMarket.intValue() > 0) {
        cri.add(Restrictions.eq("appId", appIdOfMarket));
    }
    cri.add(Restrictions.eq("apkId", apkIdOfMarket));
    cri.add(Restrictions.eq("marketName", marketName));
    cri.setCacheable(false);
    Object o = cri.uniqueResult();
    if (o != null) {
        return (MarketApp) o;
    }
    return null;
}
 
Example 12
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public List<App> listForDownloads(Session session, int currentPage, int pageSize) {
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.property("id"));
    proList.add(Projections.property("pkname"));
    proList.add(Projections.property("marketName"));

    Criteria cri = session.createCriteria(App.class);
    cri.setProjection(proList);
    cri.setMaxResults(pageSize);
    cri.setFirstResult(HibernateHelper.firstResult(currentPage, pageSize));
    // 先全部更新.
    // cri.add(Restrictions.or(Restrictions.ltProperty("realDownload",
    // "lastDayDownload"),
    // Restrictions.ne("deltaDownload", 0)));
    List<Object[]> list = HibernateHelper.list(cri);
    List<App> apps = null;
    if (list != null) {
        apps = new ArrayList<App>(list.size());
        for (Object[] obj : list) {
            App e = new App();
            e.setId((Integer) obj[0]);
            e.setPkname((String) obj[1]);
            e.setMarketName((String) obj[2]);
            apps.add(e);
        }
        list.clear();
    }
    return apps;
}
 
Example 13
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public App getAppByPackageName(Session sess, String pkname) {
    Criteria cri = sess.createCriteria(App.class);
    cri.add(Restrictions.eq("pkname", pkname));
    Object o = cri.uniqueResult();
    if (o != null) {
        return (App) o;
    }
    return null;
}
 
Example 14
Project: sdudoc   File: LogDaoImpl.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
@Override
public Pager<SysLog> listLogByUserPage(int userId, int pageNo, int pageSize) {
	Session session = sessionFactory.getCurrentSession();
	Criteria criteria = session.createCriteria(SysLog.class);
	criteria.add(Restrictions.eq("user.id", userId));
	long recordTotal = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
	criteria.setProjection(null);
	criteria.addOrder(Order.desc("time"));
	criteria.setFirstResult((pageNo - 1) * pageSize);
	criteria.setMaxResults(pageSize);
	List<SysLog> results = criteria.list();
	return new Pager<SysLog>(pageSize, pageNo, recordTotal, results);
}
 
Example 15
Project: sdudoc   File: LogDaoImpl.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
@Override
public Pager<SysLog> listLogAllByPage(int pageNo, int pageSize) {
	Session session = sessionFactory.getCurrentSession();
	Criteria criteria = session.createCriteria(SysLog.class);
	long recordTotal = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
	criteria.setProjection(null);
	criteria.addOrder(Order.desc("time"));
	criteria.setFirstResult((pageNo - 1) * pageSize);
	criteria.setMaxResults(pageSize);
	List<SysLog> results = criteria.list();
	return new Pager<SysLog>(pageSize, pageNo, recordTotal, results);
}
 
Example 16
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public long count(Session session) {
    Criteria cri = session.createCriteria(App.class);
    cri.add(Restrictions.eq("hidden", false));
    cri.setProjection(Projections.count("id"));
    Object o = cri.uniqueResult();
    return ((Long) o).longValue();
}
 
Example 17
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public Integer getPK(Session sess, String marketName, int appIdOfMarketApp) {
    Criteria cri = sess.createCriteria(MarketApp.class);
    cri.setProjection(Projections.property("id"));
    cri.add(Restrictions.eq("appId", appIdOfMarketApp));
    cri.add(Restrictions.eq("marketName", marketName));
    cri.setCacheable(false);
    Object o = cri.uniqueResult();
    if (o != null) {
        return (Integer) o;
    }
    return null;
}
 
Example 18
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public Integer getPK(Session sess, String marketName, String pkname) {
    Criteria cri = sess.createCriteria(MarketApp.class);
    cri.setProjection(Projections.property("id"));
    cri.add(Restrictions.eq("pkname", pkname));
    cri.add(Restrictions.eq("marketName", marketName));
    cri.setCacheable(false);
    Object o = cri.uniqueResult();
    if (o != null) {
        return (Integer) o;
    }
    return null;
}
 
Example 19
Project: uflo   File: ProcessQueryImpl.java   View Source Code Vote up 4 votes
public Criteria getCriteria(Session session,boolean queryCount) {
	Criteria criteria=session.createCriteria(ProcessDefinition.class);
	buildCriteria(criteria,queryCount);
	return criteria;
}
 
Example 20
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 4 votes
@Override
public List<App> getApps(int currentPage, int pageSize) {

    Session session = null;
    try {
        session = this.sessions.openSession();

        ProjectionList proList = Projections.projectionList();
        proList.add(Projections.property("id"));
        proList.add(Projections.property("name"));
        proList.add(Projections.property("catalog"));
        proList.add(Projections.property("subCatalog"));
        proList.add(Projections.property("keywords"));
        proList.add(Projections.property("description"));
        proList.add(Projections.property("downloadRank"));
        /* safe scan */
        proList.add(Projections.property("adPopupTypes"));
        proList.add(Projections.property("signatureSha1"));
        proList.add(Projections.property("officialSigSha1"));
        /* safe scan */
        proList.add(Projections.property("marketName"));
        Criteria cri = session.createCriteria(App.class);
        cri.setProjection(proList);
        cri.add(Restrictions.eq("hidden", false));
        cri.setFirstResult(HibernateHelper.firstResult(currentPage, pageSize));
        cri.setMaxResults(pageSize);
        List<Object[]> list = HibernateHelper.list(cri);
        List<App> apps = null;
        if (list != null && !list.isEmpty()) {
            apps = new ArrayList<App>(list.size());
            for (Object[] obj : list) {
                App a = new App();
                String tmp = null;
                a.setId((Integer) obj[0]);
                a.setName(((String) obj[1]).toLowerCase().trim());
                a.setCatalog((Short) obj[2]);
                a.setSubCatalog((Integer) obj[3]);
                if ((tmp = (String) obj[4]) == null) {
                    a.setKeywords("");
                } else {
                    a.setKeywords(tmp.toLowerCase().trim());
                }
                if ((tmp = (String) obj[4]) == null) {
                    a.setDescription("");
                } else {
                    a.setDescription(tmp.toLowerCase().trim());
                }
                a.setDownloadRank((Integer) obj[6]);
                /* safe scan */
                a.setAdPopupTypes((String) obj[7]);
                a.setSignatureSha1((String) obj[8]);
                a.setOfficialSigSha1((String) obj[9]);
                /* safe scan */
                a.setMarketName((String) obj[10]);
                apps.add(a);
            }
            list.clear();
        }
        return apps;
    } catch (Exception e) {
        logger.error("error:", e);
        return null;
    } finally {
        if (session != null && session.isOpen()) {
            session.clear();
            session.close();
        }
    }
}