Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

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

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:org.dafoe.framework.samples.hibernate.TerminoOntologicalSamples.java

License:Open Source License

public static void manageRTCType() {

    System.out.println("*************************** begin");

    System.out.println("RTC types creation: ");
    Session hSession = getDafoeSession();

    Transaction tx = hSession.beginTransaction();

    ITypeRelationTc test1 = new TypeRelationTcImpl();
    test1.setName("test1");
    hSession.saveOrUpdate(test1);//from w  w w . j a v a 2 s  .c  o  m

    ITypeRelationTc test2 = new TypeRelationTcImpl();
    test2.setName("test2");
    hSession.saveOrUpdate(test2);

    ITypeRelationTc test3 = new TypeRelationTcImpl();
    test3.setName("test3");
    hSession.saveOrUpdate(test3);

    tx.commit();

    System.out.println("test2 is_a test1");

    tx = hSession.beginTransaction();

    test1.addChild(test2);

    tx.commit();

    System.out.println("test2 is_a null");

    tx = hSession.beginTransaction();

    //test1.getChildren().remove(test2);

    test1.removeChild(test2);

    tx.commit();

    hSession.refresh(test2);

    System.out.println("test 1 parent " + test1.getParent());
    System.out.println("test 2 parent " + test2.getParent());
    System.out.println("test 3 parent " + test3.getParent());
    System.out.println("test 1 children " + test1.getChildren().size());
    System.out.println("test 2 children " + test2.getChildren().size());
    System.out.println("test 3 children " + test3.getChildren().size());

    System.out.println("*************************** end ");
}

From source file:org.dt.bsa.util.HibernateUtil.java

License:Open Source License

public static void refresh(Object object) throws BSAException {
    Session session = getSessionFactory().openSession();
    Transaction transaction = null;//from  w  w w  .ja  va  2s  .c  o m

    try {
        transaction = session.beginTransaction();
        session.refresh(object);
        transaction.commit();
        session.evict(object);
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        log.error("HibernateUtil:refresh:exception:" + e.getMessage());
        throw new BSAException(e.getMessage());
    } finally {
        session.close();
    }
}

From source file:org.eurocarbdb.dataaccess.HibernateEntityManager.java

License:Open Source License

public <T> void refresh(T entity) {
    if (log.isDebugEnabled())
        log.debug("attempting to refresh object of " + entity.getClass());
    Session s = getHibernateSession();
    s.refresh(entity);
}

From source file:org.freewheelschedule.freewheel.common.dao.JobDao.java

License:Apache License

public void loadLazyCollections(Job job) {
    Session session = getSession();
    session.refresh(job);
}

From source file:org.geocachingtools.geoui.util.Dao.java

/**
 * //w ww  .j a  v  a 2s  .  com
 * @param usr the user to be activated
 * @param key the key submitted by the user
 * @return true if the user got activated by the key
 */
public boolean activateUser(Gctuser usr, String key) {
    Session ses = HibernateUtil.getSessionFactory().openSession();
    Transaction tx;
    try {
        tx = ses.beginTransaction();
        Query query = ses.createQuery("" + "update Invitekey set usedby_id = :userid "
                + "where usedby_id = null " + "and invkey = :key");
        query.setParameter("userid", usr.getId());
        query.setParameter("key", key);
        int result = query.executeUpdate();
        ses.refresh(usr);
        tx.commit();
        return result == 1;
    } catch (Exception ex) {
        System.err.println("Exception in activateUser\n" + ex);
        return false;
    } finally {
        ses.close();
    }
}

From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java

License:Apache License

public void refresh(final Object entity, final LockMode lockMode) throws DataAccessException {
    doExecute(new HibernateCallback<Object>() {
        public Object doInHibernate(Session session) throws HibernateException {
            if (lockMode == null) {
                session.refresh(entity);
            } else {
                session.refresh(entity, new LockOptions(lockMode));
            }//from   w  w  w  .j av  a  2s.  co  m
            return null;
        }
    }, true);
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

public ComentForo addComment(String coment, ComentForo parent, Usuario autor) {
    if (autor.getReputacion() < minRepAddComent) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene reputacion suficiente para agregar comentarios");
    }/*  w  w  w . j  av a 2 s.c o m*/
    Session sess = sfact.getCurrentSession();
    ComentForo cf = new ComentForo();
    cf.setForo(parent.getForo());
    cf.setInReplyTo(parent);
    cf.setAutor(autor);
    cf.setFecha(new Date());
    cf.setComentario(coment);
    sess.save(cf);
    sess.flush();
    sess.refresh(parent);
    parent.getRespuestas().size();
    return cf;
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

@Transactional
public void addTag(String tag, Foro foro) {
    Session sess = sfact.getCurrentSession();
    @SuppressWarnings("unchecked")
    List<TagForo> tags = sess.createCriteria(TagForo.class).add(Restrictions.ilike("tag", tag)).setMaxResults(1)
            .list();//from   w w  w  .  j a v a2 s . c  o m
    TagForo elTag = null;
    if (tags.size() == 0) {
        elTag = new TagForo();
        elTag.setTag(tag);
        sess.save(elTag);
        sess.flush();
    } else {
        elTag = tags.get(0);
    }
    if (foro.getTags() == null) {
        sess.refresh(foro);
    }
    foro.getTags().add(elTag);
    sess.update(foro);
    foro.getTags().size();
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

public Set<ComentForo> getRespuestas(ComentForo coment) {
    Session sess = sfact.getCurrentSession();
    sess.refresh(coment);
    coment.getRespuestas().size();//from  ww w  .j  a  va  2 s .co  m
    return coment.getRespuestas();
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

public VotoForo vota(Usuario user, Foro foro, boolean up) throws PrivilegioInsuficienteException {
    if ((up && user.getReputacion() < minRepVotaFu) || (!up && user.getReputacion() < minRepVotaFd)) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene privilegio suficiente para votar por un foro");
    }/*  w  w w .  j  a  v  a 2 s . com*/
    Session sess = sfact.getCurrentSession();
    //Buscamos el voto a ver si ya se hizo
    VotoForo voto = findVoto(user, foro);
    int uprep = 0;
    if (voto == null) {
        //Si no existe lo creamos
        voto = new VotoForo();
        voto.setFecha(new Date());
        voto.setForo(foro);
        voto.setUp(up);
        voto.setUser(user);
        sess.save(voto);
        uprep = up ? 1 : -1;
    } else if (voto.isUp() != up) {
        //Si ya existe pero quieren cambio, se actualiza
        voto.setFecha(new Date());
        voto.setUp(up);
        sess.update(voto);
        uprep = up ? 2 : -2;
    }
    if (uprep != 0) {
        sess.refresh(foro);
        //Esto no es nada intuitivo pero si no lo hago asi, se arroja una excepcion marciana de Hib
        user = (Usuario) sess.merge(foro.getAutor());
        sess.lock(user, LockMode.UPGRADE);
        sess.refresh(user);
        user.setReputacion(user.getReputacion() + uprep);
        sess.update(user);
        sess.flush();
    }
    return voto;
}