List of usage examples for org.hibernate Session merge
Object merge(String entityName, Object object);
From source file:com.jdon.persistence.hibernate.HibernateTemplate.java
License:Apache License
public Object merge(final String entityName, final Object entity) throws Exception { return doHibernate(new HibernateCallback() { public Object execute(Session session) throws HibernateException { return session.merge(entityName, entity); }/* ww w. j av a2 s .c o m*/ }); }
From source file:com.oracle.coherence.hibernate.cachestore.HibernateCacheStore.java
License:CDDL license
/** * Store a Hibernate entity given an id (key) and entity (value) * <p/>//from w ww. java 2 s. co m * The entity must have an identifier attribute, and it must be either * null (undefined) or equal to the cache key. * * @param key the cache key; specifically, the entity id * @param value the cache value; specifically, the entity */ public void store(Object key, Object value) { ensureInitialized(); Transaction tx = null; Session session = openSession(); try { tx = session.beginTransaction(); validateIdentifier((Serializable) key, value, (SessionImplementor) session); // Save or Update (since we don't know if this is an insert or an // update) session.merge(getEntityName(), value); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw ensureRuntimeException(e); } finally { closeSession(session); } }
From source file:com.terp.data.CommonDaoImpl.java
License:Open Source License
/** * add or update given object and its table * @param row//from w ww .j a v a 2s . com * @return */ @Override public T addOrUpdate(T row) { // get current session Session session = HibernateUtil.getSessionFactory().openSession(); // check session if (session == null) { return null; } // begin transaction session.getTransaction().begin(); // update object T obj = (T) session.merge(this.instance.toString(), row); // commint transaction session.getTransaction().commit(); // return object return obj; }
From source file:org.eclipse.emf.cdo.server.internal.hibernate.HibernateStoreAccessor.java
License:Open Source License
/** * Performs the main write and update actions. Persists new EPackages, updates changed objects, creates new ones and * removes deleted objects. Updates both container as well as resource associations. * * @param context// w w w . ja v a2s. c o m * the context contains the changed, new and to-be-removed objects * @param monitor * not used by this method */ @Override public void doWrite(InternalCommitContext context, OMMonitor monitor) { // NOTE: the same flow is also present in the super class (StoreAccessor) // changes in flow can mean that the flow here also has to change monitor.begin(3); HibernateThreadContext.setCommitContext(context); if (context.getNewPackageUnits().length > 0) { writePackageUnits(context.getNewPackageUnits(), monitor.fork()); } // Note: instead of an Async here, we could do much more fine-grained monitoring below. But this // simplistic solution is sufficient to prevent timeout errors. final Async async = monitor.forkAsync(); HibernateThreadContext.getCommitContext().setInDoWrite(true); try { // start with fresh hibernate session to prevent side effects final Session session = context instanceof HibernateRawCommitContext ? getHibernateSession() : getNewHibernateSession(false); session.setDefaultReadOnly(false); // decrement version, hibernate will increment it decrementVersions(context); // order is 1) insert, 2) update and then delete // this order is the most stable! Do not change it without testing // System.err.println(getStore().getMappingXml()); final List<InternalCDORevision> repairContainerIDs = new ArrayList<InternalCDORevision>(); final List<InternalCDORevision> repairResourceIDs = new ArrayList<InternalCDORevision>(); for (InternalCDORevision revision : context.getNewObjects()) { revision.setListPreserving(); // keep track for which cdoRevisions the container id needs to be repaired afterwards final CDOID containerID = (CDOID) revision.getContainerID(); if (containerID instanceof CDOIDTemp && !containerID.isNull()) { repairContainerIDs.add(revision); } final CDOID resourceID = revision.getResourceID(); if (resourceID instanceof CDOIDTemp && !resourceID.isNull()) { repairResourceIDs.add(revision); } final String entityName = getStore().getEntityName(revision.getEClass()); session.saveOrUpdate(entityName, revision); } // now apply all the changes if (context.getDirtyObjectDeltas() != null) { for (InternalCDORevisionDelta delta : context.getDirtyObjectDeltas()) { final String entityName = HibernateUtil.getInstance().getEntityName(delta.getID()); final Serializable idValue = HibernateUtil.getInstance().getIdValue(delta.getID()); final InternalCDORevision cdoRevision = (InternalCDORevision) session.get(entityName, idValue); cdoRevision.setListPreserving(); delta.applyTo(cdoRevision); } } // preserve old behavior for the hibernate raw commit if (context instanceof HibernateRawCommitContext) { // now check the versions and store the hibernate revision to repair // versions later on. The versions can be updated when inserting new objects // this will result in a version difference when the object gets merged // this repair is done just before the merge final Map<CDOID, InternalCDORevision> existingRevisions = CDOIDUtil.createMap(); for (InternalCDORevision revision : context.getDirtyObjects()) { final String entityName = HibernateUtil.getInstance().getEntityName(revision.getID()); final Serializable idValue = HibernateUtil.getInstance().getIdValue(revision.getID()); final InternalCDORevision cdoRevision = (InternalCDORevision) session.get(entityName, idValue); if (cdoRevision != null) { if (cdoRevision.getVersion() != revision.getVersion()) { throw new IllegalStateException( "Revision " + cdoRevision + " was already updated by another transaction"); } existingRevisions.put(revision.getID(), cdoRevision); } } for (InternalCDORevision revision : context.getDirtyObjects()) { final String entityName = HibernateUtil.getInstance().getEntityName(revision.getID()); final InternalCDORevision existingRevision = existingRevisions.get(revision.getID()); if (existingRevision != null) { revision.setVersion(existingRevision.getVersion()); } final InternalCDORevision cdoRevision = (InternalCDORevision) session.merge(entityName, revision); if (getStore().isAuditing() && cdoRevision.getVersion() == revision.getVersion()) { // do a direct update of the version in the db to get it in sync with // hibernate, a special case, hibernate does not send the change back, do it ourselves // only needs to be done in case of auditing cdoRevision.setVersion(cdoRevision.getVersion() + 1); } if (TRACER.isEnabled()) { TRACER.trace( "Updated Object " + revision.getEClass().getName() + " id: " + revision.getID()); //$NON-NLS-1$ //$NON-NLS-2$ } } } // and increment the versions stored in the context // note that this is needed because above the cdorevision read from the db // is updated and its version gets incremented, and not the revision currently // in the cache incrementVersions(context); session.flush(); // delete all objects for (CDOID id : context.getDetachedObjects()) { try { final CDORevision revision = HibernateUtil.getInstance().getCDORevision(id); // maybe deleted in parallell? if (revision != null) { session.delete(revision); } } catch (org.hibernate.ObjectNotFoundException ex) { // ignore these, an object can be removed through cascade deletes } } session.flush(); // now do an update of the container without incrementing the version repairContainerIDs(repairContainerIDs, session); repairResourceIDs(repairResourceIDs, session); session.flush(); // write the blobs ExtendedDataInputStream in = context.getLobs(); if (in != null) { try { int count = in.readInt(); for (int i = 0; i < count; i++) { byte[] id = in.readByteArray(); long size = in.readLong(); if (size > 0) { writeBlob(id, size, new LimitedInputStream(in, size)); } else { writeClob(id, -size, new InputStreamReader(new LimitedInputStream(in, -size))); } } } catch (IOException ex) { throw WrappedException.wrap(ex); } } session.flush(); } catch (Exception e) { OM.LOG.error(e); throw WrappedException.wrap(e); } finally { HibernateThreadContext.getCommitContext().setInDoWrite(false); async.stop(); } context.applyIDMappings(monitor.fork()); monitor.done(); }
From source file:org.opengoss.dao.hibernate.DataAccessor.java
License:Apache License
public Object merge(final String entityName, final Object entity) throws DaoException { return execute(new IAccessorCallback() { public Object call(Session session) throws HibernateException { return session.merge(entityName, entity); }/*from ww w . j av a 2 s . co m*/ }); }
From source file:org.springframework.orm.hibernate3.HibernateTemplate.java
License:Apache License
@Override public <T> T merge(final String entityName, final T entity) throws DataAccessException { return executeWithNativeSession(new HibernateCallback<T>() { @Override/*from w ww. ja va2 s .c om*/ @SuppressWarnings("unchecked") public T doInHibernate(Session session) throws HibernateException { checkWriteOperationAllowed(session); return (T) session.merge(entityName, entity); } }); }
From source file:org.springframework.orm.hibernate3.StatelessHibernateTemplate.java
License:Apache License
public Object merge(final String entityName, final Object entity) throws DataAccessException { return execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { checkWriteOperationAllowed(session); return session.merge(entityName, entity); }//from w ww . j a va 2s. c o m }, true); }
From source file:org.workin.persistence.hibernate.v3.dao.Hibernate3PersistenceDaoImpl.java
License:Apache License
@Override public List<T> batchMerge(final String entityName, final List<T> entityList) { return (List<T>) getHibernateTemplate().execute(new HibernateCallback<List<T>>() { @SuppressWarnings("unchecked") @Override//from ww w.j a v a 2s .c om public List<T> doInHibernate(Session session) throws HibernateException, SQLException { List<T> list = CollectionUtils.newArrayList(); int max = entityList.size(); T entity; if (StringUtils.isNotBlank(entityName)) { for (int i = 0; i < max; i++) { entity = entityList.get(i); if (entity != null) list.add((T) session.merge(entityName, entityList.get(i))); // 1000?? if (((i != 0) && (i % 1000 == 0)) || (i == max - 1)) session.flush(); } } else { for (int i = 0; i < max; i++) { entity = entityList.get(i); if (entity != null) list.add((T) session.merge(entityList.get(i))); // 1000?? if (((i != 0) && (i % 1000 == 0)) || (i == max - 1)) session.flush(); } } return list; } }); }