Search in sources :

Example 1 with JpaCompliance

use of org.hibernate.jpa.spi.JpaCompliance in project hibernate-orm by hibernate.

the class JpaComplianceTests method testSettingTrue.

@Test
public void testSettingTrue() {
    ServiceRegistryScope.using(() -> new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_COMPLIANCE, true).build(), (serviceRegistryScope) -> {
        final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(serviceRegistryScope.getRegistry()).buildMetadata().buildSessionFactory();
        final JpaCompliance jpaCompliance = sessionFactory.getSessionFactoryOptions().getJpaCompliance();
        assertAll(jpaCompliance, true);
    });
    // MutableJpaComplianceImpl defaults its values based on the passed
    // `jpaByDefault` (`true` here).  ultimately we want to source this
    // from `AvailableSettings#JPA_COMPLIANCE`
    final MutableJpaComplianceImpl compliance = new MutableJpaComplianceImpl(Collections.emptyMap(), true);
    assertAll(compliance, true);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) MutableJpaComplianceImpl(org.hibernate.jpa.internal.MutableJpaComplianceImpl) Test(org.junit.jupiter.api.Test)

Example 2 with JpaCompliance

use of org.hibernate.jpa.spi.JpaCompliance in project hibernate-orm by hibernate.

the class IdentifierLoadAccessImpl method doLoad.

@SuppressWarnings("unchecked")
protected final T doLoad(Object id) {
    final SessionImplementor session = context.getSession();
    final EventSource eventSource = (EventSource) session;
    final LoadQueryInfluencers loadQueryInfluencers = session.getLoadQueryInfluencers();
    final JpaCompliance jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance();
    if (!jpaCompliance.isLoadByIdComplianceEnabled()) {
        id = entityPersister.getIdentifierMapping().getJavaType().coerce(id, this);
    }
    String entityName = entityPersister.getEntityName();
    Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
    if (this.lockOptions != null) {
        LoadEvent event = new LoadEvent(id, entityName, lockOptions, eventSource, readOnly);
        context.fireLoad(event, LoadEventListener.GET);
        final Object result = event.getResult();
        initializeIfNecessary(result);
        return (T) result;
    }
    LoadEvent event = new LoadEvent(id, entityName, false, eventSource, readOnly);
    boolean success = false;
    try {
        context.fireLoad(event, LoadEventListener.GET);
        success = true;
    } catch (ObjectNotFoundException e) {
    // if session cache contains proxy for non-existing object
    } finally {
        context.afterOperation(success);
    }
    final Object result = event.getResult();
    initializeIfNecessary(result);
    return (T) result;
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) EventSource(org.hibernate.event.spi.EventSource) LoadEvent(org.hibernate.event.spi.LoadEvent) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance)

Example 3 with JpaCompliance

use of org.hibernate.jpa.spi.JpaCompliance in project hibernate-orm by hibernate.

the class IdentifierLoadAccessImpl method doGetReference.

@SuppressWarnings("unchecked")
protected T doGetReference(Object id) {
    final SessionImplementor session = context.getSession();
    final EventSource eventSource = (EventSource) session;
    final LoadQueryInfluencers loadQueryInfluencers = session.getLoadQueryInfluencers();
    final JpaCompliance jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance();
    if (!jpaCompliance.isLoadByIdComplianceEnabled()) {
        id = entityPersister.getIdentifierMapping().getJavaType().coerce(id, this);
    }
    String entityName = entityPersister.getEntityName();
    Boolean readOnly = this.readOnly != null ? this.readOnly : loadQueryInfluencers.getReadOnly();
    if (this.lockOptions != null) {
        LoadEvent event = new LoadEvent(id, entityName, lockOptions, eventSource, readOnly);
        context.fireLoad(event, LoadEventListener.LOAD);
        return (T) event.getResult();
    }
    LoadEvent event = new LoadEvent(id, entityName, false, eventSource, readOnly);
    boolean success = false;
    try {
        context.fireLoad(event, LoadEventListener.LOAD);
        if (event.getResult() == null) {
            session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound(entityName, id);
        }
        success = true;
        return (T) event.getResult();
    } finally {
        context.afterOperation(success);
    }
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) EventSource(org.hibernate.event.spi.EventSource) LoadEvent(org.hibernate.event.spi.LoadEvent) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance)

Example 4 with JpaCompliance

use of org.hibernate.jpa.spi.JpaCompliance in project infinispan by infinispan.

the class TestSessionAccessImpl method mockSession.

@Override
public Object mockSession(Class<? extends JtaPlatform> jtaPlatform, ControlledTimeService timeService, RegionFactory regionFactory) {
    SessionMock session = mock(SessionMock.class);
    when(session.isClosed()).thenReturn(false);
    when(session.isOpen()).thenReturn(true);
    when(session.getTransactionStartTimestamp()).thenReturn(timeService.wallClockTime());
    TransactionCoordinator txCoord;
    if (jtaPlatform == BatchModeJtaPlatform.class) {
        BatchModeTransactionCoordinator batchModeTxCoord = new BatchModeTransactionCoordinator();
        txCoord = batchModeTxCoord;
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = batchModeTxCoord.newTransaction();
            tx.begin();
            return tx;
        });
    } else if (jtaPlatform == null || jtaPlatform == NoJtaPlatform.class) {
        Connection connection = mock(Connection.class);
        JdbcConnectionAccess jdbcConnectionAccess = mock(JdbcConnectionAccess.class);
        try {
            when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
        } catch (SQLException e) {
        // never thrown from mock
        }
        JdbcSessionOwner jdbcSessionOwner = mock(JdbcSessionOwner.class);
        when(jdbcSessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
        SqlExceptionHelper sqlExceptionHelper = mock(SqlExceptionHelper.class);
        JdbcServices jdbcServices = mock(JdbcServices.class);
        when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
        ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
        when(serviceRegistry.getService(JdbcServices.class)).thenReturn(jdbcServices);
        JdbcSessionContext jdbcSessionContext = mock(JdbcSessionContext.class);
        when(jdbcSessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
        JpaCompliance jpaCompliance = mock(JpaCompliance.class);
        when(jpaCompliance.isJpaTransactionComplianceEnabled()).thenReturn(true);
        SessionFactoryImplementor sessionFactory = mock(SessionFactoryImplementor.class);
        SessionFactoryOptions sessionFactoryOptions = mock(SessionFactoryOptions.class);
        when(sessionFactoryOptions.getJpaCompliance()).thenReturn(jpaCompliance);
        when(sessionFactory.getSessionFactoryOptions()).thenReturn(sessionFactoryOptions);
        when(jdbcSessionContext.getSessionFactory()).thenReturn(sessionFactory);
        when(jdbcSessionOwner.getJdbcSessionContext()).thenReturn(jdbcSessionContext);
        when(session.getSessionFactory()).thenReturn(sessionFactory);
        when(session.getFactory()).thenReturn(sessionFactory);
        NonJtaTransactionCoordinator txOwner = mock(NonJtaTransactionCoordinator.class);
        when(txOwner.getResourceLocalTransaction()).thenReturn(new JdbcResourceTransactionMock());
        when(txOwner.getJdbcSessionOwner()).thenReturn(jdbcSessionOwner);
        when(txOwner.isActive()).thenReturn(true);
        txCoord = JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE.buildTransactionCoordinator(txOwner, null);
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = new TransactionImpl(txCoord, session.getExceptionConverter(), session);
            tx.begin();
            return tx;
        });
    } else {
        throw new IllegalStateException("Unknown JtaPlatform: " + jtaPlatform);
    }
    Sync sync = new Sync(regionFactory);
    TestSynchronization synchronization = new TestSynchronization(sync);
    when(session.getCacheTransactionSynchronization()).thenAnswer(invocation -> {
        if (!synchronization.registered) {
            txCoord.getLocalSynchronizations().registerSynchronization(synchronization);
            synchronization.registered = true;
        }
        return sync;
    });
    return session;
}
Also used : Connection(java.sql.Connection) TestSessionAccess(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess) Sync(org.infinispan.hibernate.cache.v53.impl.Sync) JdbcResourceTransactionMock(org.infinispan.test.hibernate.cache.commons.util.JdbcResourceTransactionMock) BatchModeJtaPlatform(org.infinispan.test.hibernate.cache.commons.util.BatchModeJtaPlatform) Transaction(org.hibernate.Transaction) Query(org.hibernate.query.Query) AccessType(org.hibernate.cache.spi.access.AccessType) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Synchronization(javax.transaction.Synchronization) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) JdbcResourceTransactionAccess(org.hibernate.resource.transaction.backend.jdbc.spi.JdbcResourceTransactionAccess) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) SoftLock(org.hibernate.cache.spi.access.SoftLock) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) ControlledTimeService(org.infinispan.util.ControlledTimeService) List(java.util.List) CacheException(org.hibernate.cache.CacheException) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Mockito.mock(org.mockito.Mockito.mock) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) DomainDataRegionImpl(org.infinispan.hibernate.cache.v53.impl.DomainDataRegionImpl) DomainDataCachingConfig(org.hibernate.cache.cfg.spi.DomainDataCachingConfig) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Session(org.hibernate.Session) MetaInfServices(org.kohsuke.MetaInfServices) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess) DirectAccessRegion(org.hibernate.cache.spi.DirectAccessRegion) CachedDomainDataAccess(org.hibernate.cache.spi.access.CachedDomainDataAccess) SQLException(java.sql.SQLException) RegionFactory(org.hibernate.cache.spi.RegionFactory) TransactionCoordinatorOwner(org.hibernate.resource.transaction.spi.TransactionCoordinatorOwner) NoJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform) AbstractSharedSessionContract(org.hibernate.internal.AbstractSharedSessionContract) FlushMode(org.hibernate.FlushMode) CacheImplementor(org.hibernate.cache.spi.CacheImplementor) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Mockito.when(org.mockito.Mockito.when) ServiceRegistry(org.hibernate.service.ServiceRegistry) InfinispanBaseRegion(org.infinispan.hibernate.cache.commons.InfinispanBaseRegion) Status(javax.transaction.Status) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) SessionCreationOptions(org.hibernate.internal.SessionCreationOptions) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) JdbcResourceTransactionMock(org.infinispan.test.hibernate.cache.commons.util.JdbcResourceTransactionMock) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Connection(java.sql.Connection) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) Transaction(org.hibernate.Transaction) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) ServiceRegistry(org.hibernate.service.ServiceRegistry) Sync(org.infinispan.hibernate.cache.v53.impl.Sync)

Example 5 with JpaCompliance

use of org.hibernate.jpa.spi.JpaCompliance in project hibernate-orm by hibernate.

the class JpaComplianceTests method testSettingFalse.

@Test
public void testSettingFalse() {
    ServiceRegistryScope.using(() -> new StandardServiceRegistryBuilder().applySetting(AvailableSettings.JPA_COMPLIANCE, false).build(), (serviceRegistryScope) -> {
        final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(serviceRegistryScope.getRegistry()).buildMetadata().buildSessionFactory();
        final JpaCompliance jpaCompliance = sessionFactory.getSessionFactoryOptions().getJpaCompliance();
        assertAll(jpaCompliance, false);
    });
    // MutableJpaComplianceImpl defaults its values based on the passed
    // `jpaByDefault` (`true` here).  ultimately we want to source this
    // from `AvailableSettings#JPA_COMPLIANCE`
    final MutableJpaComplianceImpl compliance = new MutableJpaComplianceImpl(Collections.emptyMap(), true);
    assertAll(compliance, true);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) MutableJpaComplianceImpl(org.hibernate.jpa.internal.MutableJpaComplianceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

JpaCompliance (org.hibernate.jpa.spi.JpaCompliance)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 MetadataSources (org.hibernate.boot.MetadataSources)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 EventSource (org.hibernate.event.spi.EventSource)2 LoadEvent (org.hibernate.event.spi.LoadEvent)2 MutableJpaComplianceImpl (org.hibernate.jpa.internal.MutableJpaComplianceImpl)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Status (javax.transaction.Status)1 Synchronization (javax.transaction.Synchronization)1 FlushMode (org.hibernate.FlushMode)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1