Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

In this page you can find the example usage for org.hibernate Session get.

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:ch.flashcard.service.CardService.java

License:Open Source License

public final List<Card> getAllCardsByCardDeck(final int cardDeckId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();

    CardDeckEntity cardDeckEntity = (CardDeckEntity) session.get(CardDeckEntity.class, cardDeckId);
    final Criteria criteria = session.createCriteria(CardEntity.class);
    criteria.add(Restrictions.eq("carddeck", cardDeckEntity));
    List<CardEntity> cardEntities = criteria.list();
    List<Card> cards = cardMapper.mapToDomainList(cardEntities);

    transaction.commit();//w w  w  .  j  a  va2 s  .  c o m
    session.close();

    return cards;
}

From source file:ch.flashcard.service.CardServiceImpl.java

License:Open Source License

public final CardEntity getCardById(final int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();

    CardEntity cardEntity = (CardEntity) session.get(CardEntity.class, id);

    transaction.commit();//from   ww w.j a  v a2 s  . co  m
    session.close();

    return cardEntity;
}

From source file:ch.flashcard.service.CardServiceImpl.java

License:Open Source License

public final List<CardEntity> getAllCardsByCardDeck(final int cardDeckId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();

    CardDeckEntity cardDeckEntity = (CardDeckEntity) session.get(CardDeckEntity.class, cardDeckId);
    final Criteria criteria = session.createCriteria(CardEntity.class);
    criteria.add(Restrictions.eq("carddeck", cardDeckEntity));
    List<CardEntity> cardEntities = criteria.list();

    transaction.commit();//from ww  w  . j  a va2 s.co  m
    session.close();

    return cardEntities;
}

From source file:ch.icclab.cyclops.persistence.HibernateClient.java

License:Open Source License

/**
 * Access database and get object/*from  w  w w .jav  a2s.c  o m*/
 * @param clazz object
 * @param id of object
 * @return object
 */
public Object getObject(Class clazz, Long id) {
    // get session first
    Session session = obtainSession();

    // retrieve object
    Object obj = session.get(clazz, id);

    // close connection
    session.close();

    return obj;
}

From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java

License:Open Source License

static public AuditEvent findById(Long id) throws HibernateException {
    Session s = null;
    try {//from  w w w.  ja  va  2s.  c  o m
        s = openSession();
        return (AuditEvent) s.get(AuditEvent.class, id);
    } finally {
        close(s);
    }
}

From source file:chiron.maxscore.dao.impl.BaseDAOImpl.java

/**
 * ?/*from  ww  w. j av  a  2s  . c om*/
 *
 * @param id 
 * @return null
 */
@Override
public E getById(Serializable id) {
    Session session = sessionFactory.getCurrentSession();
    Object o = session.get(entityClass, id);
    return o == null ? null : (E) o;
}

From source file:chitchatserver.ConversationDAO.java

public static ChatPackage.Conversation getConversation(int conversationId) {
    ChatPackage.Conversation rs = null;/*from   w  ww.j av  a 2  s. c o m*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        rs = (ChatPackage.Conversation) session.get(ChatPackage.Conversation.class, conversationId);
    } catch (Exception ex) {
        System.out.println("error getting conversation");
    } finally {
        session.close();
    }
    return rs;
}

From source file:chitchatserver.SentenceDAO.java

public static ChatPackage.Sentence getSentence(int id) {
    Sentence rs = null;//from  w w  w  .  j  a  v a  2  s  . co m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        rs = (Sentence) session.get(Sentence.class, id);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    } finally {
        session.close();
    }
    return rs;
}

From source file:chitchatserver.UserDAO.java

public static ChatPackage.User getUser(String userId) {
    ChatPackage.User rs = null;/*  ww w. j  av a 2  s .c  o m*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        rs = (ChatPackage.User) session.get(ChatPackage.User.class, userId);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    } finally {
        session.close();
    }
    return rs;
}

From source file:Ciclo.buscaCiclo.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    t_busca.requestFocus();/*from w  ww.j a va2s . c om*/
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);
    Session session = HibernateUtil.getSessionFactory().openSession();
    usr = (Usuario) session.get(Usuario.class, usr.getIdUsuario());
    if (usr.getEditaPeriodo() == true) {
        if (t_datos.getRowCount() > 0) {
            if (t_datos.getSelectedRow() >= 0) {
                Ciclo cic = new Ciclo();
                cic.setIdCiclo(Integer.parseInt(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString()));
                cic.setDescripcion(t_datos.getValueAt(t_datos.getSelectedRow(), 1).toString());
                if (session != null)
                    if (session.isOpen())
                        session.close();
                doClose(cic);
            } else
                JOptionPane.showMessageDialog(null, "No hay un Catlogo seleccionado!");
        }
    } else
        JOptionPane.showMessageDialog(null, "Acceso denegado!");
    if (session != null)
        if (session.isOpen())
            session.close();
}