Java Code Examples for org.hibernate.Query.uniqueResult()

The following are Jave code examples for showing how to use uniqueResult() of the org.hibernate.Query 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: ImageClassify   File: UserService.java   View Source Code Vote up 8 votes
/**
 * 获取冻结未冻结用户的总数
 */
@Override
public long getUsersNumOfFrozenOrUnfrozen(int is_frozen) {
	 long num =0l;
	 Session session = HibernateUtils.getSession();//生成Session实例
	 Transaction tx = session.beginTransaction();//生成事务实例 
	 try { 
	    Query query = session.createQuery("SELECT count(*) from User where is_frozen = ?"); 
		    query.setInteger(0, is_frozen);
	    num = (long) query.uniqueResult();
	     //调用session的get()方法,找到此用户到内存中
	    tx.commit();//提交事务
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();//事务回滚
	}finally{
		HibernateUtils.closeSession();//关闭session实例
	}   
	 return num;
}
 
Example 2
Project: lams   File: EventDAOHibernate.java   View Source Code Vote up 7 votes
@Override
   public long getPendingNotificationCount(Long lessonId, Integer userId) {
String query = EventDAOHibernate.COUNT_PENDING_NOTIFICATIONS;
if (lessonId != null) {
    query += " AND s.event.eventSessionId = ?";
}
Query queryObject = getSession().createQuery(query);
queryObject.setInteger(0, userId);
if (lessonId != null) {
    queryObject.setLong(1, lessonId);
}
return (Long) queryObject.uniqueResult();
   }
 
Example 3
Project: sjk   File: CatalogConvertorDaoImpl.java   View Source Code Vote up 5 votes
@Override
public boolean isExists(String marketName, short catalog, int subCatalog) {
    Query query = getSession()
            .createQuery(
                    "select count(id) from CatalogConvertor where   catalog= :catalog and marketName= :marketName and subCatalog =:subCatalog")
            .setInteger("catalog", catalog)
            .setString("marketName", StringUtils.defaultIfEmpty(marketName, "").trim())
            .setInteger("subCatalog", subCatalog);
    Object o = query.uniqueResult();
    return Integer.valueOf(o.toString()) > 0;
}
 
Example 4
Project: lams   File: LessonDAO.java   View Source Code Vote up 5 votes
/**
    * @see org.lamsfoundation.lams.lesson.dao.ILessonDAO#getActiveLearnerByLesson(long) Note: Hibernate 3.1
    *      query.uniqueResult() returns Integer, Hibernate 3.2 query.uniqueResult() returns Long
    */
   @Override
   public Integer getCountActiveLearnerByLesson(long lessonId) {
Query query = getSession().createQuery(LessonDAO.COUNT_ACTIVE_LEARNERS);
query.setLong("lessonId", lessonId);
Object value = query.uniqueResult();
return new Integer(((Number) value).intValue());
   }
 
Example 5
Project: Restaurant   File: OrderDaoImpl.java   View Source Code Vote up 5 votes
public Order getOrderDetails(int orderId){
	String sql = "from Order where orderId=:orderId";
	Session session = this.sessionFactory.openSession();
	Query query = session.createQuery(sql);
	query.setParameter("orderId", orderId);
	
	Order order = (Order) query.uniqueResult();
	session.close();
	return order;
	
}
 
Example 6
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public long countDownloads(Session session, String marketName) {
    Query query = session.createQuery("select sum(downloads) from MarketApp where marketName =:marketName");
    query.setParameter("marketName", marketName);
    Object o = query.uniqueResult();
    if (o != null) {
        return Long.valueOf(o.toString());
    }
    return 0;
}
 
Example 7
Project: sjk   File: SysDictionaryDaoImpl.java   View Source Code Vote up 5 votes
@Override
public int getAppsRollRecommendNum() {
    Query query = getSession().createQuery("select intValue from SysDictionary where name = 'recommendCount'");
    Object o = query.uniqueResult();
    if (o != null) {
        return ((Integer) o).intValue();
    }
    return 0;
}
 
Example 8
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public int countDownloads(String pkname, int id) {
    Query query = getSession().createQuery(
            "select sum(downloads) from MarketApp where pkname =:pkname and id <> :id");
    query.setParameter("id", id);
    query.setParameter("pkname", pkname);
    Object o = query.uniqueResult();
    if (o != null) {
        return Integer.valueOf(o.toString());
    }
    return 0;
}
 
Example 9
Project: lams   File: AssessmentQuestionResultDAOHibernate.java   View Source Code Vote up 5 votes
@Override
   public Float getQuestionResultMark(Long assessmentUid, Long userId, int questionSequenceId) {
Query q = getSession().createQuery(GET_ANSWER_MARK);
q.setParameter(0, assessmentUid);
q.setParameter(1, userId);
q.setParameter(2, questionSequenceId);
q.setMaxResults(1);
Object result = q.uniqueResult();
return result != null ? ((Number) result).floatValue() : null;

   }
 
Example 10
Project: lams   File: UserDAO.java   View Source Code Vote up 5 votes
@Override
   public int getCountUsers(String searchPhrase) {
StringBuilder queryBuilder = new StringBuilder("SELECT count(*) FROM User user WHERE user.disabledFlag=0 ");
// support for custom search from a toolbar
UserDAO.addNameSearch(queryBuilder, "user", searchPhrase);

Query query = getSession().createQuery(queryBuilder.toString());

Number count = (Number) query.uniqueResult();
return count == null ? 0 : count.intValue();
   }
 
Example 11
Project: lams   File: AssessmentResultDAOHibernate.java   View Source Code Vote up 5 votes
@Override
   public Float getLastTotalScoreByUser(Long assessmentUid, Long userId) {

Query q = getSession().createQuery(LAST_ASSESSMENT_RESULT_GRADE);
q.setParameter(0, userId);
q.setParameter(1, assessmentUid);
Object lastTotalScore = q.uniqueResult();

return (lastTotalScore == null) ? 0 : (Float)lastTotalScore;
   }
 
Example 12
Project: sjk   File: KeywordDaoImpl.java   View Source Code Vote up 5 votes
@Override
public boolean exists(String name) {
    String queryString = "select count(id) from Keyword where name = :name ";
    Query q = getSession().createQuery(queryString);
    q.setParameter("name", name);
    Long count = (Long) q.uniqueResult();
    if (count != null && count.longValue() > 0L) {
        return true;
    }
    return false;
}
 
Example 13
Project: sjk   File: MoMixFeaturedDaoImpl.java   View Source Code Vote up 5 votes
@Override
public long count(String type) {
    StringBuilder queryString = new StringBuilder("select count(id) from ").append(getType().getName());
    queryString.append(" where type = :type and hidden = 0");
    Query query = getSession().createQuery(queryString.toString());
    query.setParameter("type", type);
    Object o = query.uniqueResult();
    return Long.valueOf(o.toString());
}
 
Example 14
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 4 votes
@Override
public long count() {
    Query query = getSession().createQuery("select count(id) from App");
    Object o = query.uniqueResult();
    return Long.valueOf(o.toString());
}
 
Example 15
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 4 votes
@Override
public long count() {
    Query query = getSession().createQuery("select count(id) from App where hidden = 0");
    Object o = query.uniqueResult();
    return Long.valueOf(o.toString());
}
 
Example 16
Project: DWSurvey   File: HibernateDao.java   View Source Code Vote up 4 votes
@Override
public Object findUniObjs(String hql,Object... values ) {
	Query q = createQuery(hql, values);
	return q.uniqueResult();
}
 
Example 17
Project: sjk   File: CatalogConvertorDaoImpl.java   View Source Code Vote up 4 votes
@Override
public long count() {
    Query query = getSession().createQuery("select count(id) from CatalogConvertor");
    Object o = query.uniqueResult();
    return Integer.valueOf(o.toString());
}
 
Example 18
Project: Library   File: LibraryServicesImpl.java   View Source Code Vote up 3 votes
public RestResponse checkInBook(CheckInBook book) {

		RestResponse response = new RestResponse();
		Session session = this.sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		try {

			String paramString = "from BookLoan where book.ISBN='" + book.getIsbn() + "' and borrower.cardId="
					+ Integer.parseInt(book.getCardId());
			Query query = session.createQuery(paramString);

			BookLoan bookLoan = (BookLoan) query.uniqueResult();

			bookLoan.setDateIn(new Date());

			bookLoan.getBook().setAvailable(true);

			session.update(bookLoan);
			transaction.commit();

			response.setSuccess(true);
			response.setResult("Book Checked In Succesfully");
		} catch (Exception exception) {

			response.setSuccess(false);
			response.setError("Error Occurred. Try after some time");
			transaction.rollback();
		}

		session.close();

		return response;

	}
 
Example 19
Project: Celebino   File: UserDao.java   View Source Code Vote up 3 votes
/**
 * get user by email
 * 
 * @param email
 * @return
 * @throws SQLException
 */
public User getByEmail(String email) throws SQLException {

	Session session = HibernateUtil.getSessionFactory().openSession();

	User user = null;

	try {

		String hql = "from User as a" + " where a.email like :email";

		Query query = session.createQuery(hql);
		query.setParameter("email", "%" + email + "%");

		user = (User) query.uniqueResult();

	} catch (HibernateException hibernateException) {

		session.getTransaction().rollback();

		throw new SQLException(hibernateException);

	} finally {

		session.close();
	}

	return user;
}
 
Example 20
Project: Celebino   File: UserDao.java   View Source Code Vote up 3 votes
/**
 * Get user by username
 * 
 * @param username
 * @return
 * @throws SQLException
 */
public User getByUsername(String username) throws SQLException {

	Session session = HibernateUtil.getSessionFactory().openSession();

	User user = null;

	try {

		String hql = "from User as a" + " where a.username like :username";

		Query query = session.createQuery(hql);
		query.setParameter("username", "%" + username + "%");

		user = (User) query.uniqueResult();

	} catch (HibernateException hibernateException) {

		session.getTransaction().rollback();

		throw new SQLException(hibernateException);

	} finally {

		session.close();
	}

	return user;
}