Search in sources :

Example 31 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class OnLockVisitor method processCollection.

@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == null) {
        return null;
    }
    final SessionImplementor session = getSession();
    final CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
    if (collection instanceof PersistentCollection) {
        final PersistentCollection persistentCollection = (PersistentCollection) collection;
        if (persistentCollection.setCurrentSession(session)) {
            if (isOwnerUnchanged(persistentCollection, persister, extractCollectionKeyFromOwner(persister))) {
                // a "detached" collection that originally belonged to the same entity
                if (persistentCollection.isDirty()) {
                    throw new HibernateException("reassociated object has dirty collection");
                }
                reattachCollection(persistentCollection, type);
            } else {
                // a "detached" collection that belonged to a different entity
                throw new HibernateException("reassociated object has dirty collection reference");
            }
        } else {
            // to the entity passed to update()
            throw new HibernateException("reassociated object has dirty collection reference");
        }
    } else {
        //TODO: or an array!! we can't lock objects with arrays now??
        throw new HibernateException("reassociated object has dirty collection reference (or an array)");
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 32 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class AbstractManipulationCriteriaQuery method interpret.

@Override
public CriteriaInterpretation interpret(RenderingContext renderingContext) {
    final String jpaqlString = renderQuery(renderingContext);
    return new CriteriaInterpretation() {

        @Override
        @SuppressWarnings("unchecked")
        public QueryImplementor buildCompiledQuery(SessionImplementor entityManager, final InterpretedParameterMetadata interpretedParameterMetadata) {
            final Map<String, Class> implicitParameterTypes = extractTypeMap(interpretedParameterMetadata.implicitParameterBindings());
            QueryImplementor query = entityManager.createQuery(jpaqlString, null, null, new HibernateEntityManagerImplementor.QueryOptions() {

                @Override
                public List<ValueHandlerFactory.ValueHandler> getValueHandlers() {
                    return null;
                }

                @Override
                public Map<String, Class> getNamedParameterExplicitTypes() {
                    return implicitParameterTypes;
                }

                @Override
                public ResultMetadataValidator getResultMetadataValidator() {
                    return null;
                }
            });
            for (ImplicitParameterBinding implicitParameterBinding : interpretedParameterMetadata.implicitParameterBindings()) {
                implicitParameterBinding.bind(query);
            }
            return query;
        }

        private Map<String, Class> extractTypeMap(List<ImplicitParameterBinding> implicitParameterBindings) {
            final HashMap<String, Class> map = new HashMap<>();
            for (ImplicitParameterBinding implicitParameter : implicitParameterBindings) {
                map.put(implicitParameter.getParameterName(), implicitParameter.getJavaType());
            }
            return map;
        }
    };
}
Also used : HashMap(java.util.HashMap) CriteriaInterpretation(org.hibernate.query.criteria.internal.compile.CriteriaInterpretation) HibernateEntityManagerImplementor(org.hibernate.jpa.spi.HibernateEntityManagerImplementor) InterpretedParameterMetadata(org.hibernate.query.criteria.internal.compile.InterpretedParameterMetadata) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) QueryImplementor(org.hibernate.query.spi.QueryImplementor) HashMap(java.util.HashMap) Map(java.util.Map) ImplicitParameterBinding(org.hibernate.query.criteria.internal.compile.ImplicitParameterBinding)

Example 33 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class TransactionTimeoutTest method testTransactionTimeoutFailure.

@Test
public void testTransactionTimeoutFailure() throws InterruptedException {
    Session session = openSession();
    try {
        Transaction transaction = session.getTransaction();
        transaction.setTimeout(1);
        assertEquals(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
        transaction.begin();
        Thread.sleep(1000);
        session.persist(new Person("Lukasz", "Antoniak"));
        transaction.commit();
    } catch (TransactionException e) {
    // expected
    } catch (PersistenceException e) {
        assertTyping(TransactionException.class, e.getCause());
    } finally {
        session.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Person(org.hibernate.test.jdbc.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class AuditProcessManager method get.

public AuditProcess get(EventSource session) {
    final Transaction transaction = session.accessTransaction();
    AuditProcess auditProcess = auditProcesses.get(transaction);
    if (auditProcess == null) {
        // No worries about registering a transaction twice - a transaction is single thread
        auditProcess = new AuditProcess(revisionInfoGenerator, session);
        auditProcesses.put(transaction, auditProcess);
        session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {

            public void doBeforeTransactionCompletion(SessionImplementor session) {
                final AuditProcess process = auditProcesses.get(transaction);
                if (process != null) {
                    process.doBeforeTransactionCompletion(session);
                }
            }
        });
        session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

            public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                auditProcesses.remove(transaction);
            }
        });
    }
    return auditProcess;
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) Transaction(org.hibernate.Transaction) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 35 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class CrossTypeRevisionChangesReaderImpl method findEntityTypes.

@Override
@SuppressWarnings({ "unchecked" })
public Set<Pair<String, Class>> findEntityTypes(Number revision) throws IllegalStateException, IllegalArgumentException {
    checkNotNull(revision, "Entity revision");
    checkPositive(revision, "Entity revision");
    checkSession();
    final Session session = auditReaderImplementor.getSession();
    final SessionImplementor sessionImplementor = auditReaderImplementor.getSessionImplementor();
    final Set<Number> revisions = new HashSet<>(1);
    revisions.add(revision);
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
    final Object revisionInfo = query.uniqueResult();
    if (revisionInfo != null) {
        // If revision exists.
        final Set<String> entityNames = enversService.getModifiedEntityNamesReader().getModifiedEntityNames(revisionInfo);
        if (entityNames != null) {
            // Generate result that contains entity names and corresponding Java classes.
            final Set<Pair<String, Class>> result = new HashSet<>();
            for (String entityName : entityNames) {
                result.add(Pair.make(entityName, EntityTools.getEntityClass(sessionImplementor, entityName)));
            }
            return result;
        }
    }
    return Collections.EMPTY_SET;
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) HashSet(java.util.HashSet) Pair(org.hibernate.envers.tools.Pair)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)82 Session (org.hibernate.Session)59 Test (org.junit.Test)54 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 EntityEntry (org.hibernate.engine.spi.EntityEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)6