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:br.com.bean.RestControllers.usuarioController.java

public static Usuario buscaUsuarioParaOperacao(long id) throws HibernateException {
    Session sessao = HibernateUtility.getSession();
    try {//from   ww w.j  a  v  a 2 s . c o m
        Usuario u = (Usuario) sessao.get(Usuario.class, id);
        return u;
    } catch (HibernateException e) {

    } finally {
        sessao.close();
    }
    return null;
}

From source file:br.com.bean.RestControllers.vendaController.java

@RequestMapping(value = "buscaPorId-venda", method = RequestMethod.GET)
public String get(long id) throws HibernateException {
    Session sessao = HibernateUtility.getSession();
    try {//from  www  .j  a v  a 2s  . c  om
        Venda v = (Venda) sessao.get(Pagamento.class, id);
        Gson gson = new Gson();
        return gson.toJson(v, Venda.class);
    } catch (HibernateException e) {
        return CriadorJson.criaJsonErro(e, "Verificar");
    } finally {
        sessao.close();
    }
}

From source file:br.com.bean.RestControllers.vendaController.java

public static Venda buscaVendaParaOperacao(long id) throws HibernateException {
    Session sessao = HibernateUtility.getSession();
    try {/* w w w .ja  va2  s  .  c  o m*/
        Venda v = (Venda) sessao.get(Venda.class, id);
        return v;
    } catch (HibernateException e) {

    } finally {
        sessao.close();
    }
    return null;
}

From source file:br.com.bean.RestControllers.viagemController.java

@RequestMapping(value = "buscaPorId-viagem", method = RequestMethod.GET)
public String get(long id) throws HibernateException {
    Session sessao = HibernateUtility.getSession();
    try {//from ww w. ja va2 s  .  c  o m
        Viagem v = (Viagem) sessao.get(Viagem.class, id);
        Gson gson = new Gson();
        return gson.toJson(v, Viagem.class);
    } catch (HibernateException e) {
        return CriadorJson.criaJsonErro(e, "Verificar");
    } finally {
        sessao.close();
    }
}

From source file:br.com.bean.RestControllers.viagemController.java

public Viagem buscaViagemParaOperacao(long id) throws HibernateException {
    Session sessao = HibernateUtility.getSession();
    try {//w  w  w.  j  a v a  2s.  c om
        Viagem v = (Viagem) sessao.get(Viagem.class, id);
        return v;
    } catch (HibernateException e) {

    } finally {
        sessao.close();
    }
    return null;
}

From source file:br.com.bluesoft.pronto.dao.HibernateAuditLogListener.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w  w  w .  ja  v a  2s  .  co m
public final boolean onPreUpdate(final PreUpdateEvent event) {

    Session session = null;

    try {

        if (!event.getEntity().getClass().equals(Ticket.class)) {
            return false;
        }

        final Serializable entityId = event.getPersister().hasIdentifierProperty() ? event.getPersister()
                .getIdentifier(event.getEntity(), event.getPersister().guessEntityMode(event.getEntity()))
                : null;
        final Date transTime = new Date();
        final EntityMode entityMode = event.getPersister().guessEntityMode(event.getEntity());
        Object oldPropValue = null;
        Object newPropValue = null;

        session = event.getPersister().getFactory().openSession();
        final Transaction tx = session.beginTransaction();

        final Object existingEntity = session.get(event.getEntity().getClass(), entityId);

        if (existingEntity != null) {

            for (final String propertyName : event.getPersister().getPropertyNames()) {

                String campo = propertyName;
                final boolean temLabel = Ticket.class.getDeclaredField(propertyName)
                        .isAnnotationPresent(Label.class);
                if (temLabel) {
                    campo = Ticket.class.getDeclaredField(propertyName).getAnnotation(Label.class).value();
                }

                newPropValue = event.getPersister().getPropertyValue(event.getEntity(), propertyName,
                        entityMode);
                if (newPropValue != null) {
                    final boolean ehUmaCollection = newPropValue instanceof Collection;
                    if (!ehUmaCollection) {
                        oldPropValue = event.getPersister().getPropertyValue(existingEntity, propertyName,
                                entityMode);

                        final String oldValue = makeString(oldPropValue);
                        final String newValue = makeString(newPropValue);

                        if (!oldValue.equals(newValue)) {
                            final TicketLog history = new TicketLog();
                            history.setTicket((Ticket) event.getEntity());
                            history.setData(transTime);
                            history.setUsuario(Seguranca.getUsuario().getUsername());
                            history.setCampo(campo);
                            history.setValorAntigo(oldValue);
                            history.setValorNovo(newValue);
                            history.setOperacao(TicketLog.ALTERACAO);
                            if (history.isDiferente()) {
                                session.save(history);
                            }
                        }
                    }
                }
            }
        }

        tx.commit();
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return false;
}

From source file:br.com.Dao.HibernateDao.java

@Override
public T getEntity(Serializable id) {
    T entity = (T) Session.get(classe, id);
    return entity;
}

From source file:br.com.moises.dao.Dao.java

@Override
public T getEntity(Serializable id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {//from  w  w  w. j av  a2 s  . com
        session.getTransaction().begin();
        T entity = (T) session.get(classe, id);
        return entity;
    } catch (Exception e) {
        return null;
    } finally {
        session.getTransaction().commit();
    }
}

From source file:br.com.proj.tasker.dao.impl.CartaoCredDAO.java

@Override
public Cartaocred listarPorId(Integer id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Cartaocred card = (Cartaocred) session.get(Cartaocred.class, id);
    session.close();//from   w w w . j av a 2  s.  c  om
    return card;
}

From source file:br.com.proj.tasker.dao.impl.EnderecoDAO.java

@Override
public Endereco listarPorId(Integer id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Endereco end = (Endereco) session.get(Endereco.class, id);
    session.close();/*  w  w w. j  av a 2 s .c om*/
    return end;
}