Skip to content

Commit cc750a9

Browse files
dreab8beikov
authored andcommitted
Fix Session#setProperty() for cache and timeout related properties
1 parent c02ce35 commit cc750a9

File tree

2 files changed

+160
-4
lines changed

2 files changed

+160
-4
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,19 +2584,36 @@ public void setProperty(String propertyName, Object value) {
25842584
setHibernateFlushMode( ConfigurationHelper.getFlushMode(value, FlushMode.AUTO) );
25852585
break;
25862586
case JPA_LOCK_SCOPE:
2587-
case JPA_LOCK_TIMEOUT:
25882587
case JAKARTA_LOCK_SCOPE:
2588+
properties.put( JPA_LOCK_SCOPE, value );
2589+
properties.put( JAKARTA_LOCK_SCOPE, value );
2590+
LockOptionsHelper.applyPropertiesToLockOptions(properties, this::getLockOptionsForWrite);
2591+
break;
2592+
case JPA_LOCK_TIMEOUT:
25892593
case JAKARTA_LOCK_TIMEOUT:
2594+
properties.put( JPA_LOCK_TIMEOUT, value );
2595+
properties.put( JAKARTA_LOCK_TIMEOUT, value );
25902596
LockOptionsHelper.applyPropertiesToLockOptions(properties, this::getLockOptionsForWrite);
25912597
break;
25922598
case JPA_SHARED_CACHE_RETRIEVE_MODE:
2593-
case JPA_SHARED_CACHE_STORE_MODE:
25942599
case JAKARTA_SHARED_CACHE_RETRIEVE_MODE:
2600+
properties.put( JPA_SHARED_CACHE_RETRIEVE_MODE, value );
2601+
properties.put( JAKARTA_SHARED_CACHE_RETRIEVE_MODE, value );
2602+
setCacheMode(
2603+
CacheModeHelper.interpretCacheMode(
2604+
determineCacheStoreMode( properties ),
2605+
(CacheRetrieveMode) value
2606+
)
2607+
);
2608+
break;
2609+
case JPA_SHARED_CACHE_STORE_MODE:
25952610
case JAKARTA_SHARED_CACHE_STORE_MODE:
2611+
properties.put( JPA_SHARED_CACHE_STORE_MODE, value );
2612+
properties.put( JAKARTA_SHARED_CACHE_STORE_MODE, value );
25962613
setCacheMode(
25972614
CacheModeHelper.interpretCacheMode(
2598-
determineCacheStoreMode(properties),
2599-
determineCacheRetrieveMode(properties)
2615+
(CacheStoreMode) value,
2616+
determineCacheRetrieveMode( properties )
26002617
)
26012618
);
26022619
break;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.jpa.compliance;
8+
9+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
10+
import org.hibernate.testing.orm.junit.Jpa;
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import jakarta.persistence.Cache;
15+
import jakarta.persistence.CacheRetrieveMode;
16+
import jakarta.persistence.CacheStoreMode;
17+
import jakarta.persistence.Cacheable;
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.SharedCacheMode;
21+
import jakarta.persistence.Table;
22+
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
26+
@Jpa(
27+
annotatedClasses = CacheTest.Person.class,
28+
sharedCacheMode = SharedCacheMode.ALL
29+
)
30+
public class CacheTest {
31+
32+
@AfterEach
33+
public void tearDown(EntityManagerFactoryScope scope) {
34+
scope.inTransaction(
35+
entityManager ->
36+
entityManager.createQuery( "delete from Person" ).executeUpdate()
37+
);
38+
}
39+
40+
@Test
41+
public void testSettingCacheRetrieveModeToBypass(EntityManagerFactoryScope scope) {
42+
43+
scope.inTransaction(
44+
entityManager -> {
45+
Person p = new Person( 1, "Andrea" );
46+
entityManager.persist( p );
47+
48+
}
49+
);
50+
51+
scope.getEntityManagerFactory().getCache().evictAll();
52+
53+
scope.inEntityManager(
54+
entityManager -> {
55+
Cache cache = entityManager.getEntityManagerFactory().getCache();
56+
assertNotNull( cache );
57+
assertFalse( cache.contains( Person.class, 1 ) );
58+
59+
entityManager.setProperty(
60+
"jakarta.persistence.cache.retrieveMode",
61+
CacheRetrieveMode.BYPASS
62+
);
63+
entityManager.setProperty(
64+
"jakarta.persistence.cache.storeMode",
65+
CacheStoreMode.BYPASS
66+
);
67+
68+
entityManager.find( Person.class, 1 );
69+
70+
cache = entityManager.getEntityManagerFactory().getCache();
71+
assertNotNull( cache );
72+
assertFalse( cache.contains( Person.class, 1 ) );
73+
}
74+
);
75+
}
76+
77+
@Test
78+
public void testSettingCacheStoreModeToBypass(EntityManagerFactoryScope scope) {
79+
scope.inEntityManager(
80+
entityManager -> {
81+
try {
82+
entityManager.getTransaction().begin();
83+
entityManager.setProperty(
84+
"jakarta.persistence.cache.storeMode",
85+
CacheStoreMode.BYPASS
86+
);
87+
Person p = new Person( 1, "Andrea" );
88+
entityManager.persist( p );
89+
entityManager.flush();
90+
entityManager.getTransaction().commit();
91+
}
92+
finally {
93+
if ( entityManager.getTransaction().isActive() ) {
94+
entityManager.getTransaction().rollback();
95+
}
96+
}
97+
Cache cache = entityManager.getEntityManagerFactory().getCache();
98+
assertNotNull( cache );
99+
assertFalse( cache.contains( Person.class, 1 ) );
100+
101+
final Person person = entityManager.find( Person.class, 1 );
102+
assertNotNull( person );
103+
cache = entityManager.getEntityManagerFactory().getCache();
104+
assertNotNull( cache );
105+
assertFalse( cache.contains( Person.class, 1 ) );
106+
}
107+
);
108+
109+
}
110+
111+
@Entity(name = "Person")
112+
@Table(name = "PERSON_TABLE")
113+
@Cacheable
114+
public static class Person {
115+
116+
@Id
117+
private Integer id;
118+
119+
private String name;
120+
121+
public Person() {
122+
}
123+
124+
public Person(Integer id, String name) {
125+
this.id = id;
126+
this.name = name;
127+
}
128+
129+
public Integer getId() {
130+
return id;
131+
}
132+
133+
public String getName() {
134+
return name;
135+
}
136+
}
137+
138+
139+
}

0 commit comments

Comments
 (0)