Example usage for org.hibernate Query uniqueResult

List of usage examples for org.hibernate Query uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Query uniqueResult.

Prototype

R uniqueResult();

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

/**
 * ?HQL?/*from  w  ww  .  j a  v a2 s.  com*/
 *
 * @param hql
 * @param params
 * @return
 */
protected P get(String hql, Map<String, Object> params) {
    Assert.hasLength(hql);
    P t = null;
    try {
        Query query = HibernateUtils.getHqlQuery(hql, slaveSession(), isCurrent());
        setParamMap(params, query);
        t = (P) query.uniqueResult();
    } catch (Exception e) {
        logger.warn("Warn: Unexpected exception.  Cause:" + e);
    }
    return t;

}

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

@Override
public int selectCount(Wrapper wrapper) {
    int count = 0;
    try {//  w  w w .j  av a2s  . c o  m
        String sql = SqlUtils.sqlCount(poClass(), wrapper);
        Query query = HibernateUtils.getSqlQuery(sql, slaveSession(), isCurrent());
        BigInteger bigInteger = (BigInteger) query.uniqueResult();
        count = bigInteger.intValue();
    } catch (Exception e) {
        logger.warn("Warn: Unexpected exception.  Cause:" + e);
    }
    return count;
}

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

/**
 * ?HQL?/*w w  w.jav a 2  s.  c  om*/
 *
 * @param hql
 * @param params
 * @return
 */
protected int queryCountWithHql(String hql, Map<String, Object> params) {
    Assert.hasLength(hql);
    Query query = HibernateUtils.getHqlQuery(hql, slaveSession(), isCurrent());
    setParamMap(params, query);
    BigInteger bigInteger = (BigInteger) query.uniqueResult();
    return bigInteger.intValue();
}

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

/**
 * SQL?/*from  w  w w . j a  v a2s  .  c o  m*/
 *
 * @param sql
 * @return
 */
protected int queryCount(String sql, Wrapper wrapper) {
    Assert.hasLength(sql);
    sql += wrapper.getSqlSegment();
    int count = 0;
    try {
        Query query = HibernateUtils.getSqlQuery(sql, slaveSession(), isCurrent());
        BigInteger bigInteger = (BigInteger) query.uniqueResult();
        bigInteger.intValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

/**
 * SQL?//  ww w  .  j a v  a 2  s .co m
 *
 * @param sql
 * @param wrapper
 * @return
 */
protected Map<String, Object> queryMap(String sql, Wrapper wrapper) {
    Assert.hasLength(sql);
    Map<String, Object> map = Collections.emptyMap();
    try {
        sql += wrapper.getSqlSegment();
        Query query = HibernateUtils.getSqlQuery(sql, slaveSession(), isCurrent())
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        map = (Map<String, Object>) query.uniqueResult();
    } catch (Exception e) {
        logger.warn("Warn: Unexpected exception.  Cause:" + e);
    }
    return map;
}

From source file:com.baomidou.hibernateplus.dao.impl.DaoImpl.java

License:Open Source License

/**
 * Page total//ww  w . j a  v a2 s  .  c o m
 *
 * @param sql
 * @param page
 */
private void setPageTotal(String sql, Page<?> page) {
    if (page.isSearchCount()) {
        String sqlCount = SqlUtils.sqlCountOptimize(sql);
        Query countQuery = HibernateUtils.getSqlQuery(sqlCount, slaveSession(), isCurrent());
        BigInteger bigInteger = (BigInteger) countQuery.uniqueResult();
        page.setTotal(bigInteger.intValue());
    }
}

From source file:com.Bean.CommentinfoHelper.java

public String getCommentUsername(int commentid) {
    String username;/*ww w . j  a  v a 2s .  c  o  m*/
    try {
        Query q = session.createQuery("select u.username from User u, Comment c where c.cmtId =" + commentid
                + "and c.userId = u.uid");
        username = q.uniqueResult().toString();
        //            System.out.println(username);
        return username;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.Bean.ContactinfoHelper.java

public String getContactPhone(int userid) {
    String phone;/*from www.  ja  va 2s .co m*/
    try {
        Query q = session.createQuery("select c.call1 from ContactInfo c where c.userId =" + userid);

        phone = q.uniqueResult().toString();
        System.out.println(phone);
        return phone;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.Bean.ContactinfoHelper.java

public String getContactEmail(int userid) {
    System.out.println(userid);//from   w  w w  .ja  v a  2  s. co m
    String email;
    try {
        Query q = session.createQuery("select c.email1 from ContactInfo c where c.userId =" + userid);
        System.out.print(q);
        email = q.uniqueResult().toString();
        System.out.println(email);
        return email;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.Bean.PostinfoHelper.java

public String getPostUsername(int postid) {
    String username;/*ww  w  .j  av  a  2s .c o  m*/
    try {
        Query q = session.createQuery(
                "select u.username from User u, Post p where p.postId =" + postid + "and p.userId = u.uid");

        username = q.uniqueResult().toString();
        //            System.out.println(username);
        return username;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}