Search in sources :

Example 1 with ReplicationMode

use of org.hibernate.ReplicationMode in project hibernate-orm by hibernate.

the class DefaultReplicateEventListener method onReplicate.

/**
 * Handle the given replicate event.
 *
 * @param event The replicate event to be handled.
 *
 * @throws TransientObjectException An invalid attempt to replicate a transient entity.
 */
public void onReplicate(ReplicateEvent event) {
    final EventSource source = event.getSession();
    if (source.getPersistenceContext().reassociateIfUninitializedProxy(event.getObject())) {
        LOG.trace("Uninitialized proxy passed to replicate()");
        return;
    }
    Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
    if (source.getPersistenceContext().isEntryFor(entity)) {
        LOG.trace("Ignoring persistent instance passed to replicate()");
        // hum ... should we cascade anyway? throw an exception? fine like it is?
        return;
    }
    EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
    // get the id from the object
    /*if ( persister.isUnsaved(entity, source) ) {
			throw new TransientObjectException("transient instance passed to replicate()");
		}*/
    Serializable id = persister.getIdentifier(entity, source);
    if (id == null) {
        throw new TransientObjectException("instance with null id passed to replicate()");
    }
    final ReplicationMode replicationMode = event.getReplicationMode();
    final Object oldVersion;
    if (replicationMode == ReplicationMode.EXCEPTION) {
        // always do an INSERT, and let it fail by constraint violation
        oldVersion = null;
    } else {
        // what is the version on the database?
        oldVersion = persister.getCurrentVersion(id, source);
    }
    final boolean traceEnabled = LOG.isTraceEnabled();
    if (oldVersion != null) {
        if (traceEnabled) {
            LOG.tracev("Found existing row for {0}", MessageHelper.infoString(persister, id, source.getFactory()));
        }
        // / HHH-2378
        final Object realOldVersion = persister.isVersioned() ? oldVersion : null;
        boolean canReplicate = replicationMode.shouldOverwriteCurrentVersion(entity, realOldVersion, persister.getVersion(entity), persister.getVersionType());
        // else do nothing (don't even reassociate object!)
        if (canReplicate) {
            performReplication(entity, id, realOldVersion, persister, replicationMode, source);
        } else if (traceEnabled) {
            LOG.trace("No need to replicate");
        }
    // TODO: would it be better to do a refresh from db?
    } else {
        // no existing row - do an insert
        if (traceEnabled) {
            LOG.tracev("No existing row, replicating new instance {0}", MessageHelper.infoString(persister, id, source.getFactory()));
        }
        // prefer re-generation of identity!
        final boolean regenerate = persister.isIdentifierAssignedByInsert();
        final EntityKey key = regenerate ? null : source.generateEntityKey(id, persister);
        performSaveOrReplicate(entity, key, persister, regenerate, replicationMode, source, true);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TransientObjectException(org.hibernate.TransientObjectException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable) ReplicationMode(org.hibernate.ReplicationMode)

Aggregations

Serializable (java.io.Serializable)1 ReplicationMode (org.hibernate.ReplicationMode)1 TransientObjectException (org.hibernate.TransientObjectException)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 EventSource (org.hibernate.event.spi.EventSource)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1