Fix Session#setProperty() for cache and timeout related properties

This commit is contained in:
Andrea Boriero 2022-02-16 18:46:03 +01:00 committed by Christian Beikov
parent c02ce35aa0
commit cc750a9abd
2 changed files with 161 additions and 5 deletions

View File

@ -2584,19 +2584,36 @@ public class SessionImpl
setHibernateFlushMode( ConfigurationHelper.getFlushMode(value, FlushMode.AUTO) );
break;
case JPA_LOCK_SCOPE:
case JPA_LOCK_TIMEOUT:
case JAKARTA_LOCK_SCOPE:
properties.put( JPA_LOCK_SCOPE, value );
properties.put( JAKARTA_LOCK_SCOPE, value );
LockOptionsHelper.applyPropertiesToLockOptions(properties, this::getLockOptionsForWrite);
break;
case JPA_LOCK_TIMEOUT:
case JAKARTA_LOCK_TIMEOUT:
properties.put( JPA_LOCK_TIMEOUT, value );
properties.put( JAKARTA_LOCK_TIMEOUT, value );
LockOptionsHelper.applyPropertiesToLockOptions(properties, this::getLockOptionsForWrite);
break;
case JPA_SHARED_CACHE_RETRIEVE_MODE:
case JPA_SHARED_CACHE_STORE_MODE:
case JAKARTA_SHARED_CACHE_RETRIEVE_MODE:
case JAKARTA_SHARED_CACHE_STORE_MODE:
properties.put( JPA_SHARED_CACHE_RETRIEVE_MODE, value );
properties.put( JAKARTA_SHARED_CACHE_RETRIEVE_MODE, value );
setCacheMode(
CacheModeHelper.interpretCacheMode(
determineCacheStoreMode(properties),
determineCacheRetrieveMode(properties)
determineCacheStoreMode( properties ),
(CacheRetrieveMode) value
)
);
break;
case JPA_SHARED_CACHE_STORE_MODE:
case JAKARTA_SHARED_CACHE_STORE_MODE:
properties.put( JPA_SHARED_CACHE_STORE_MODE, value );
properties.put( JAKARTA_SHARED_CACHE_STORE_MODE, value );
setCacheMode(
CacheModeHelper.interpretCacheMode(
(CacheStoreMode) value,
determineCacheRetrieveMode( properties )
)
);
break;

View File

@ -0,0 +1,139 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.jpa.compliance;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Cache;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.Table;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Jpa(
annotatedClasses = CacheTest.Person.class,
sharedCacheMode = SharedCacheMode.ALL
)
public class CacheTest {
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
public void testSettingCacheRetrieveModeToBypass(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Person p = new Person( 1, "Andrea" );
entityManager.persist( p );
}
);
scope.getEntityManagerFactory().getCache().evictAll();
scope.inEntityManager(
entityManager -> {
Cache cache = entityManager.getEntityManagerFactory().getCache();
assertNotNull( cache );
assertFalse( cache.contains( Person.class, 1 ) );
entityManager.setProperty(
"jakarta.persistence.cache.retrieveMode",
CacheRetrieveMode.BYPASS
);
entityManager.setProperty(
"jakarta.persistence.cache.storeMode",
CacheStoreMode.BYPASS
);
entityManager.find( Person.class, 1 );
cache = entityManager.getEntityManagerFactory().getCache();
assertNotNull( cache );
assertFalse( cache.contains( Person.class, 1 ) );
}
);
}
@Test
public void testSettingCacheStoreModeToBypass(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
try {
entityManager.getTransaction().begin();
entityManager.setProperty(
"jakarta.persistence.cache.storeMode",
CacheStoreMode.BYPASS
);
Person p = new Person( 1, "Andrea" );
entityManager.persist( p );
entityManager.flush();
entityManager.getTransaction().commit();
}
finally {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
}
Cache cache = entityManager.getEntityManagerFactory().getCache();
assertNotNull( cache );
assertFalse( cache.contains( Person.class, 1 ) );
final Person person = entityManager.find( Person.class, 1 );
assertNotNull( person );
cache = entityManager.getEntityManagerFactory().getCache();
assertNotNull( cache );
assertFalse( cache.contains( Person.class, 1 ) );
}
);
}
@Entity(name = "Person")
@Table(name = "PERSON_TABLE")
@Cacheable
public static class Person {
@Id
private Integer id;
private String name;
public Person() {
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
}
}