Search in sources :

Example 1 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class CacheKeySerializationTest method testId.

private void testId(CacheKeysFactory cacheKeysFactory, String entityName, Object id) throws Exception {
    final SessionFactoryImplementor sessionFactory = getSessionFactory(cacheKeysFactory.getClass().getName());
    final EntityPersister persister = sessionFactory.getEntityPersister(entityName);
    final Object key = cacheKeysFactory.createEntityKey(id, persister, sessionFactory, null);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(key);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Object keyClone = ois.readObject();
    try {
        assertEquals(key, keyClone);
        assertEquals(keyClone, key);
        assertEquals(key.hashCode(), keyClone.hashCode());
        final Object idClone = cacheKeysFactory.getEntityId(keyClone);
        assertEquals(id.hashCode(), idClone.hashCode());
        assertEquals(id, idClone);
        assertEquals(idClone, id);
        assertTrue(persister.getIdentifierType().isEqual(id, idClone, sessionFactory));
        assertTrue(persister.getIdentifierType().isEqual(idClone, id, sessionFactory));
        sessionFactory.close();
    } finally {
        sessionFactory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityInsertAction method execute.

@Override
public void execute() throws HibernateException {
    nullifyTransientReferencesIfNotAlready();
    final EntityPersister persister = getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Object instance = getInstance();
    final Serializable id = getId();
    final boolean veto = preInsert();
    if (!veto) {
        persister.insert(id, getState(), instance, session);
        PersistenceContext persistenceContext = session.getPersistenceContext();
        final EntityEntry entry = persistenceContext.getEntry(instance);
        if (entry == null) {
            throw new AssertionFailure("possible non-threadsafe access to session");
        }
        entry.postInsert(getState());
        if (persister.hasInsertGeneratedProperties()) {
            persister.processInsertGeneratedProperties(id, instance, getState(), session);
            if (persister.isVersionPropertyGenerated()) {
                version = Versioning.getVersion(getState(), persister);
            }
            entry.postUpdate(instance, getState(), version);
        }
        persistenceContext.registerInsertedKey(persister, getId());
    }
    final SessionFactoryImplementor factory = session.getFactory();
    if (isCachePutEnabled(persister, session)) {
        final CacheEntry ce = persister.buildCacheEntry(instance, getState(), version, session);
        cacheEntry = persister.getCacheEntryStructure().structure(ce);
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(id, persister, factory, session.getTenantIdentifier());
        final boolean put = cacheInsert(persister, ck);
        if (put && factory.getStatistics().isStatisticsEnabled()) {
            factory.getStatistics().secondLevelCachePut(cache.getRegion().getName());
        }
    }
    handleNaturalIdPostSaveNotifications(id);
    postInsert();
    if (factory.getStatistics().isStatisticsEnabled() && !veto) {
        factory.getStatistics().insertEntity(getPersister().getEntityName());
    }
    markExecuted();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry)

Example 3 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityVerifyVersionProcess method doBeforeTransactionCompletion.

@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
    final EntityPersister persister = entry.getPersister();
    if (!entry.isExistsInDatabase()) {
        // HHH-9419: We cannot check for a version of an entry we ourselves deleted
        return;
    }
    final Object latestVersion = persister.getCurrentVersion(entry.getId(), session);
    if (!entry.getVersion().equals(latestVersion)) {
        throw new OptimisticLockException(object, "Newer version [" + latestVersion + "] of entity [" + MessageHelper.infoString(entry.getEntityName(), entry.getId()) + "] found in database");
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) OptimisticLockException(org.hibernate.OptimisticLockException)

Example 4 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityDeleteAction method execute.

@Override
public void execute() throws HibernateException {
    final Serializable id = getId();
    final EntityPersister persister = getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Object instance = getInstance();
    final boolean veto = preDelete();
    Object version = this.version;
    if (persister.isVersionPropertyGenerated()) {
        // we need to grab the version value from the entity, otherwise
        // we have issues with generated-version entities that may have
        // multiple actions queued during the same flush
        version = persister.getVersion(instance);
    }
    final Object ck;
    if (persister.hasCache()) {
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        ck = cache.generateCacheKey(id, persister, session.getFactory(), session.getTenantIdentifier());
        lock = cache.lockItem(session, ck, version);
    } else {
        ck = null;
    }
    if (!isCascadeDeleteEnabled && !veto) {
        persister.delete(id, version, instance, session);
    }
    //postDelete:
    // After actually deleting a row, record the fact that the instance no longer 
    // exists on the database (needed for identity-column key generation), and
    // remove it from the session cache
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityEntry entry = persistenceContext.removeEntry(instance);
    if (entry == null) {
        throw new AssertionFailure("possible nonthreadsafe access to session");
    }
    entry.postDelete();
    persistenceContext.removeEntity(entry.getEntityKey());
    persistenceContext.removeProxy(entry.getEntityKey());
    if (persister.hasCache()) {
        persister.getCacheAccessStrategy().remove(session, ck);
    }
    persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference(persister, id, naturalIdValues);
    postDelete();
    if (getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto) {
        getSession().getFactory().getStatistics().deleteEntity(getPersister().getEntityName());
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 5 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityIdentityInsertAction method execute.

@Override
public void execute() throws HibernateException {
    nullifyTransientReferencesIfNotAlready();
    final EntityPersister persister = getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Object instance = getInstance();
    final boolean veto = preInsert();
    if (!veto) {
        generatedId = persister.insert(getState(), instance, session);
        if (persister.hasInsertGeneratedProperties()) {
            persister.processInsertGeneratedProperties(generatedId, instance, getState(), session);
        }
        //need to do that here rather than in the save event listener to let
        //the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
        persister.setIdentifier(instance, generatedId, session);
        session.getPersistenceContext().registerInsertedKey(getPersister(), generatedId);
        entityKey = session.generateEntityKey(generatedId, persister);
        session.getPersistenceContext().checkUniqueness(entityKey, getInstance());
    }
    //TODO: this bit actually has to be called afterQuery all cascades!
    //      but since identity insert is called *synchronously*,
    //      instead of asynchronously as other actions, it isn't
    /*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
			cacheEntry = new CacheEntry(object, persister, session);
			persister.getCache().insert(generatedId, cacheEntry);
		}*/
    postInsert();
    if (session.getFactory().getStatistics().isStatisticsEnabled() && !veto) {
        session.getFactory().getStatisticsImplementor().insertEntity(getPersister().getEntityName());
    }
    markExecuted();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)166 Test (org.junit.Test)59 Serializable (java.io.Serializable)34 Session (org.hibernate.Session)31 Type (org.hibernate.type.Type)29 EntityEntry (org.hibernate.engine.spi.EntityEntry)21 EventSource (org.hibernate.event.spi.EventSource)15 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 TestForIssue (org.hibernate.testing.TestForIssue)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)12 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)12 EntityType (org.hibernate.type.EntityType)12 ArrayList (java.util.ArrayList)11 CompositeType (org.hibernate.type.CompositeType)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 EntityKey (org.hibernate.engine.spi.EntityKey)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)8