Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:org.cgiar.ccafs.marlo.data.dao.impl.StandardDAO.java

License:Open Source License

/**
 * This method deletes a record from the database.
 * /* w  w  w.  j  a va2 s  . c  o  m*/
 * @param obj is a persistence instance from the database model.
 * @return true if the record was successfully deleted, false otherwhise.
 */
protected boolean delete(Object obj) {
    Session session = null;
    Transaction tx = null;
    try {
        session = this.openSession(interceptor);
        interceptor.setSession(session);
        Object newEntityRef = session.merge(obj);
        tx = this.initTransaction(session);
        session.clear();
        session.delete(newEntityRef);
        this.commitTransaction(tx);

        return true;
    } catch (Exception e) {
        if (tx != null) {
            this.rollBackTransaction(tx);
        }
        e.printStackTrace();
        return false;
    } finally {
        // Flushing the changes always.
    }
}

From source file:org.cgiar.ccafs.marlo.data.dao.impl.StandardDAO.java

License:Open Source License

/**
 * This method saves or update a record into the database.
 * //from  w  ww  .java  2 s.  c  o  m
 * @param obj is the Object to be saved/updated.
 * @return true if the the save/updated was successfully made, false otherwhise.
 */
protected boolean update(Object obj) {
    Session session = null;
    Transaction tx = null;
    try {
        session = this.openSession();
        // interceptor.setSession(session);

        tx = this.initTransaction(session);

        obj = session.merge(obj);
        session.saveOrUpdate(obj);
        this.commitTransaction(tx);

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        if (tx != null) {
            this.rollBackTransaction(tx);
        }
        session.clear();
        if (e instanceof org.hibernate.exception.ConstraintViolationException) {
            Transaction tx1 = session.beginTransaction();
            tx1.commit();
        }
        return false;
    }
}

From source file:org.cgiar.ccafs.marlo.data.dao.impl.StandardDAO.java

License:Open Source License

/**
 * This method saves or update a record into the database.
 * //  w ww . j  a  va2  s .co  m
 * @param obj is the Object to be saved/updated.
 * @param actionName the action that called the save
 * @return true if the the save/updated was successfully made, false otherwhise.
 */
protected boolean update(Object obj, String actionName, List<String> relationsName) {
    Session session = null;
    Transaction tx = null;
    try {
        session = this.openSession(interceptor);
        interceptor.setSession(session);
        interceptor.setActionName(actionName);
        interceptor.setRelationsName(relationsName);
        tx = this.initTransaction(session);

        obj = session.merge(obj);
        session.saveOrUpdate(obj);

        this.commitTransaction(tx);

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        if (tx != null) {
            this.rollBackTransaction(tx);
        }
        session.clear();
        if (e instanceof org.hibernate.exception.ConstraintViolationException) {
            Transaction tx1 = session.beginTransaction();
            tx1.commit();
        }
        return false;
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.metaclass.MergePersistentMethod.java

License:Apache License

@Override
protected Object performSave(final Object target, final boolean flush) {
    return getHibernateTemplate().execute(new HibernateCallback<Object>() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Object merged = session.merge(target);
            session.lock(merged, LockMode.NONE);

            if (flush) {
                getHibernateTemplate().flush();
            }//from  w ww . j  a va2 s.com
            return merged;
        }
    });
}

From source file:org.eclipse.emf.teneo.hibernate.test.issues.UpdateAction.java

License:Open Source License

@Override
public void doAction(TestStore store) {
    {/*from w ww . ja  v a 2  s . co  m*/
        final Parent p = UpdateFactory.eINSTANCE.createParent();
        p.setName("p");
        p.setDescription("original");
        final Child c1 = UpdateFactory.eINSTANCE.createChild();
        c1.setName("c1");
        c1.setDescription("original");
        final Child c2 = UpdateFactory.eINSTANCE.createChild();
        c2.setName("c2");
        c2.setDescription("original");
        p.getChildren().add(c1);
        p.getChildren().add(c2);
        store.beginTransaction();
        store.store(p.getChildren());
        store.store(p);
        store.commitTransaction();
    }

    {
        final Parent p = UpdateFactory.eINSTANCE.createParent();
        p.setName("p");
        p.setDescription("updated");
        final Child c1 = UpdateFactory.eINSTANCE.createChild();
        c1.setName("c1");
        c1.setDescription("updated");
        final Child c2 = UpdateFactory.eINSTANCE.createChild();
        c2.setName("c3");
        c2.setDescription("updated");
        p.getChildren().add(c1);
        p.getChildren().add(c2);
        store.beginTransaction();
        final Session session = ((HibernateTestStore) store).getSession();
        session.merge(p);
        store.commitTransaction();
    }

    {
        final Parent p = UpdateFactory.eINSTANCE.createParent();
        p.setName("p2");
        p.setDescription("o2");
        final Child c1 = UpdateFactory.eINSTANCE.createChild();
        c1.setName("c4");
        c1.setDescription("o2");
        final Child c2 = UpdateFactory.eINSTANCE.createChild();
        c2.setName("c5");
        c2.setDescription("o2");
        p.getChildren().add(c1);
        p.getChildren().add(c2);
        store.beginTransaction();
        final Session session = ((HibernateTestStore) store).getSession();
        session.merge(p);
        store.commitTransaction();
    }
}

From source file:org.etk.component.database.impl.HibernateServiceImpl.java

License:Open Source License

public Object save(Object obj) throws Exception {
    Session session = openSession();
    session.merge(obj);
    session.flush();/*ww w. java 2s . c o m*/
    return obj;
}

From source file:org.etk.core.membership.hibernate.UserDAOImpl.java

License:Open Source License

public void saveUser(User user, boolean broadcast) throws Exception {
    if (broadcast)
        preSave(user, false);/*from  w ww  .j  a  va 2  s .  com*/
    Session session = service_.openSession();
    session.merge(user);
    // session.update(user);
    if (broadcast)
        postSave(user, false);
    session.flush();
    cache_.put(user.getUserName(), user);
}

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

License:Open Source License

public <T> void store(T entity) {
    if (log.isDebugEnabled())
        log.debug("attempting to store (make persistent) object " + entity);

    Session s = getHibernateSession();

    if (entity instanceof EurocarbObject)
        validate((EurocarbObject) entity);

    try {/*from   w w  w . ja va  2  s.c o  m*/
        s.save(entity);
        //s.saveOrUpdate( entity );
    } catch (NonUniqueObjectException ex) {
        log.warn("passed " + entity.getClass().getSimpleName() + " was originally loaded in a different Session"
                + "; attempting to merge changes...", ex);

        s.merge(entity);
        log.debug("changes merged ok");
    }
}

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

License:Open Source License

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

From source file:org.exoplatform.common.dao.hibernate.HibernateJpaRepository.java

License:Open Source License

/**
 * <p>//  w w  w.  ja  v  a  2  s  . c om
 * Copy the state of the given object onto the persistent object with the
 * same identifier. If there is no persistent instance currently associated
 * with the session, it will be loaded. Return the persistent instance. If
 * the given instance is unsaved, save a copy of and return it as a newly
 * persistent instance. The given instance does not become associated with
 * the session. This operation cascades to associated instances if the
 * association is mapped with cascade="merge".
 * 
 * <p>
 * The difference between <code>update()</code> and <code>merge()</code> is
 * significant: <code>update()</code> will make the given object persistent
 * and throw and error if another object with the same ID is already
 * persistent in the Session. <code>merge()</code> doesn't care if another
 * object is already persistent, but it also doesn't make the given object
 * persistent; it just copies over the values to the datastore.
 */
public <T> T _mergeEntity(T entity) {
    Session session = openSession();
    T t = null;
    try {
        t = (T) session.merge(entity);
        session.flush();
        session.clear();
    } finally {
        session.close();
    }
    return t;
}