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:app.SpHelper.java

public Sanpham getSPByID(int SpId) {
    Sanpham sanpham = null;/*  w w w . j a  va2 s  . c o m*/
    try {
        org.hibernate.Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from Sanpham as sanpham where sanpham.maSp=" + SpId);
        sanpham = (Sanpham) q.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sanpham;
}

From source file:applicationController.Handlers.GetAllChatsHandler.java

@Override
public void handleIt(HashMap inMap, JSONOutputStream outToClient) {
    Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = session.createQuery("FROM Meeting WHERE meeting_id = :meeting ");
    query.setParameter("meeting", this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
    Meeting meeting = (Meeting) query.uniqueResult();
    String returnMsg = "";
    if (meeting != null) {
        for (Message msg : meeting.getMessages()) {
            returnMsg += msg.getUsername() + ": " + msg.getMsg() + "\n";
        }//  w  w  w. j  ava 2 s . co m
    } else {
        Meeting newMeeting = new Meeting();
        newMeeting.setMeeting_id(this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
        newMeeting.setName("not named yet");
        session.save(newMeeting);
    }

    session.getTransaction().commit();

    this.commBean.setCommand("SENDCHAT");
    HashMap data = new HashMap();
    data.put("msg", returnMsg);
    this.commBean.setData(data);

    try {
        outToClient.writeObject(this.commBean);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:applicationController.Handlers.SendChatHandler.java

@Override
public void handleIt(HashMap inMap, JSONOutputStream outToClient) {
    Session session = HibernateUtilSingleton.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = session.createQuery("FROM Meeting WHERE meeting_id = :meeting ");
    query.setParameter("meeting", this.commBean.convertLong(((HashMap) inMap.get("data")).get("meeting")));
    Meeting meeting = (Meeting) query.uniqueResult();
    Message msg = new Message();
    msg.setMeeting(meeting);//  w  ww  .  j  a v  a2  s.  c  o  m
    msg.setUsername(((HashMap) inMap.get("data")).get("username").toString());
    msg.setMsg(((HashMap) inMap.get("data")).get("msg").toString());
    session.save(msg);
    session.getTransaction().commit();

    this.commBean.setCommand("SENDCHAT");
    HashMap data = new HashMap();
    data.put("msg", ((HashMap) inMap.get("data")).get("msg").toString());
    data.put("username", ((HashMap) inMap.get("data")).get("username").toString());
    this.commBean.setData(data);

    try {
        outToClient.writeObject(this.commBean);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ar.com.zauber.commons.web.cache.impl.repo.hibernate.HibernateLastModifiedRepository.java

License:Apache License

/** @see LastModifiedRepository#getMaxTimestamp(List) */
public final Long getMaxTimestamp(final List<StringEntityKey> keys) {
    if (keys.size() == 0) {
        return this.getTimestamp(keys.get(0));
    }//from  ww w  .jav  a 2s.co m

    String[] keyStrings = new String[keys.size()];
    for (int i = 0; i < keys.size(); i++) {
        keyStrings[i] = keys.get(i).getAsString();
    }

    Query query = this.sessionFactory.getCurrentSession()
            .getNamedQuery("lastModified.orderedTimestampsForEntities");

    query.setParameterList("entitiesKey", keyStrings);
    query.setMaxResults(1);
    return (Long) query.uniqueResult();
}

From source file:ar.com.zauber.commons.web.cache.impl.repo.hibernate.HibernateLastModifiedRepository.java

License:Apache License

/** @see LastModifiedRepository#getTimestamp(EntityKey) */
@Transactional(readOnly = true)//ww w .j a v  a 2  s .  c o  m
public final Long getTimestamp(final StringEntityKey key) {
    Validate.notNull(key);
    Query query = this.sessionFactory.getCurrentSession().getNamedQuery("lastModified.timestampForEntity");

    query.setString("entityKey", key.getAsString());
    return (Long) query.uniqueResult();
}

From source file:aseguradora.VistaVentana.java

private void eliminarAsegurado() {
    try {/*from  w w  w  .j a va 2 s . c om*/
        Session session = sesion.openSession();
        Transaction tx = session.beginTransaction();
        PolizasAsegurados pa;

        Query q = session
                .createQuery("from PolizasAsegurados as pa " + "where pa.id.codP = ? " + "and pa.id.num = ?");

        q.setInteger(0, Integer.parseInt(etCodPoliza.getText()));
        q.setInteger(1, Integer.parseInt(etNumAsegurado.getText()));

        pa = (PolizasAsegurados) q.uniqueResult();

        session.delete(pa);
        tx.commit();
        session.close();
        cargarVista();
    } catch (JDBCException e) {
        e.printStackTrace();
        if (e.getErrorCode() == ERROR_EDITAR_CLAVE_AJENA) {
            JOptionPane.showMessageDialog(rootPane,
                    "No puedes borrar este asegurado, restriccin clave primeria/ajena");
        }
    }
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@Override//w w  w  . j  av  a 2 s. co  m
public Long findEffortsSumForTiTATasks(Long projectId, Long userId, Long taskId) {
    String queryString = "select sum(e.duration) as sum " + "from Effort e " + "join e.titaTask as tt "
            + "join tt.titaProject as tp " + "where e.user = " + userId + " and tp.id = " + projectId
            + " and tt.id=" + taskId + " and e.deleted != true";

    Query q = getSession().createQuery(queryString);

    Long result = (Long) q.uniqueResult();
    return (result != null) ? result : 0L;
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@Override/* ww w .j  av  a  2 s.  com*/
public Long findEffortsSumForIssueTrackerTasks(Long projectId, Long userId, Long taskId) {
    String queryString = "select sum(e.duration) as sum " + "from Effort e " + "join e.issueTTask as itt "
            + "join itt.isstProject as itp " + "join itp.titaProject as tp " + "where e.user = " + userId
            + " and tp.id = " + projectId + " and itt.id= " + taskId + " and e.deleted != true";

    Query q = getSession().createQuery(queryString);

    Long result = (Long) q.uniqueResult();
    return (result != null) ? result : 0L;
}

From source file:at.ac.tuwien.ifs.tita.dao.time.EffortDao.java

License:Apache License

/** {@inheritDoc} */
@Override/*from ww  w .j a  v  a  2s  . c  om*/
public Long totalizeEffortsForTiTAProjectAndTiTAUser(Long projectId, Long userId) {

    String first = "select sum(e.duration) as sum " + "from Effort e " + "join e.titaTask as tt "
            + "join tt.titaProject as tp " + "where e.user = " + userId + " and tp.id = " + projectId
            + " and e.deleted != true";

    String second = "select sum(e.duration) as sum " + "from Effort e " + "join e.issueTTask as itt "
            + "join itt.isstProject as itp " + "join itp.titaProject as tp " + "where e.user = " + userId
            + " and tp.id = " + projectId + " and e.deleted != true";

    Query query1 = getSession().createQuery(first);
    Query query2 = getSession().createQuery(second);
    Long sumQuery1 = (Long) query1.uniqueResult();
    Long sumQuery2 = (Long) query2.uniqueResult();

    if (sumQuery1 != null && sumQuery2 != null) {
        return sumQuery1 + sumQuery2;
    } else if (sumQuery1 != null && sumQuery2 == null) {
        return sumQuery1;
    } else if (sumQuery1 == null && sumQuery2 != null) {
        return sumQuery2;
    } else {
        return 0L;
    }
}

From source file:at.ac.tuwien.ifs.tita.dao.user.UserDAO.java

License:Apache License

/** {@inheritDoc} */
@Override//www . j a  v  a2  s . c  o  m
public Long findTargetHoursForTiTAProjectAndTiTAUser(Long userId, Long projectId) {

    String queryString = "select tup.targetHours " + "from TiTAUserProject tup " + "where tup.user = " + userId
            + " and tup.project = " + projectId;

    Long targetHours;
    try {
        Query query = getSession().createQuery(queryString);
        targetHours = (Long) query.uniqueResult();
    } catch (ObjectDeletedException e) {
        return null;
    }

    if (targetHours == null || targetHours == -1) {
        return null;
    } else {
        return targetHours;
    }
}