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();
}
}
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();
}
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");
}
}
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());
}
}
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();
}
Aggregations