Fixed - Cache isn't applied to hibernate collection, that is joined by non primary key field #2708
parent
e197c43107
commit
c7036d9339
@ -1,8 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
singleServerConfig:
|
||||
address: "redis://127.0.0.1:6379"
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2013-2020 Nikita Koksharov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.hibernate.PropertyNotFoundException;
|
||||
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
import org.redisson.client.codec.Codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class RedissonCacheKeysFactory extends DefaultCacheKeysFactory {
|
||||
|
||||
private final Codec codec;
|
||||
|
||||
public RedissonCacheKeysFactory(Codec codec) {
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
|
||||
try {
|
||||
String[] parts = persister.getRole().split("\\.");
|
||||
Field f = ReflectHelper.findField(id.getClass(), parts[parts.length - 1]);
|
||||
|
||||
Object prev = f.get(id);
|
||||
f.set(id, null);
|
||||
ByteBuf state = codec.getMapKeyEncoder().encode(id);
|
||||
Object newId = codec.getMapKeyDecoder().decode(state, null);
|
||||
state.release();
|
||||
f.set(id, prev);
|
||||
return super.createCollectionKey(newId, persister, factory, tenantIdentifier);
|
||||
} catch (PropertyNotFoundException e) {
|
||||
return super.createCollectionKey(id, persister, factory, tenantIdentifier);
|
||||
} catch (IllegalAccessException | IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.stat.Statistics;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class CollectionTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class A implements Serializable {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Column(unique = true) String uniqueField;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "a_b",
|
||||
joinColumns = @JoinColumn(
|
||||
name = "unique_field", referencedColumnName = "uniqueField"),
|
||||
inverseJoinColumns = @JoinColumn(
|
||||
name = "b_id", referencedColumnName = "id"))
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
private List<B> bs = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class B implements Serializable {
|
||||
@Id Long id;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { A.class, B.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration cfg) {
|
||||
super.configure(cfg);
|
||||
cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
|
||||
cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
|
||||
cfg.setProperty(Environment.USER, "sa");
|
||||
cfg.setProperty(Environment.PASS, "");
|
||||
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
|
||||
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
|
||||
|
||||
cfg.setProperty(Environment.SHOW_SQL, "true");
|
||||
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
|
||||
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
|
||||
cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
sessionFactory().getCache().evictEntityRegions();
|
||||
sessionFactory().getStatistics().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() {
|
||||
Statistics stats = sessionFactory().getStatistics();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
A a = new A();
|
||||
a.id = 1L;
|
||||
a.uniqueField = "1";
|
||||
B b = new B();
|
||||
b.id = 1L;
|
||||
s.save(b);
|
||||
a.bs.add(b);
|
||||
s.save(a);
|
||||
s.flush();
|
||||
s.getTransaction().commit();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a1 = s.get(A.class, 1L);
|
||||
System.out.println("here1");
|
||||
assertThat(a1.bs).hasSize(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
Assert.assertEquals(0, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a2 = s.get(A.class, 1L);
|
||||
B b2 = a2.bs.iterator().next();
|
||||
assertThat(a2.bs.size()).isEqualTo(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
s.close();
|
||||
|
||||
Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
stats.logSummary();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2013-2020 Nikita Koksharov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.hibernate.PropertyNotFoundException;
|
||||
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
import org.redisson.client.codec.Codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class RedissonCacheKeysFactory extends DefaultCacheKeysFactory {
|
||||
|
||||
private final Codec codec;
|
||||
|
||||
public RedissonCacheKeysFactory(Codec codec) {
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
|
||||
try {
|
||||
String[] parts = persister.getRole().split("\\.");
|
||||
Field f = ReflectHelper.findField(id.getClass(), parts[parts.length - 1]);
|
||||
|
||||
Object prev = f.get(id);
|
||||
f.set(id, null);
|
||||
ByteBuf state = codec.getMapKeyEncoder().encode(id);
|
||||
Object newId = codec.getMapKeyDecoder().decode(state, null);
|
||||
state.release();
|
||||
f.set(id, prev);
|
||||
return super.createCollectionKey(newId, persister, factory, tenantIdentifier);
|
||||
} catch (PropertyNotFoundException e) {
|
||||
return super.createCollectionKey(id, persister, factory, tenantIdentifier);
|
||||
} catch (IllegalAccessException | IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.stat.Statistics;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class CollectionTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class A implements Serializable {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Column(unique = true) String uniqueField;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "a_b",
|
||||
joinColumns = @JoinColumn(
|
||||
name = "unique_field", referencedColumnName = "uniqueField"),
|
||||
inverseJoinColumns = @JoinColumn(
|
||||
name = "b_id", referencedColumnName = "id"))
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
private List<B> bs = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class B implements Serializable {
|
||||
@Id Long id;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { A.class, B.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration cfg) {
|
||||
super.configure(cfg);
|
||||
cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
|
||||
cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
|
||||
cfg.setProperty(Environment.USER, "sa");
|
||||
cfg.setProperty(Environment.PASS, "");
|
||||
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
|
||||
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
|
||||
|
||||
cfg.setProperty(Environment.SHOW_SQL, "true");
|
||||
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
|
||||
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
|
||||
cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
sessionFactory().getCache().evictEntityRegions();
|
||||
sessionFactory().getStatistics().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() {
|
||||
Statistics stats = sessionFactory().getStatistics();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
A a = new A();
|
||||
a.id = 1L;
|
||||
a.uniqueField = "1";
|
||||
B b = new B();
|
||||
b.id = 1L;
|
||||
s.save(b);
|
||||
a.bs.add(b);
|
||||
s.save(a);
|
||||
s.flush();
|
||||
s.getTransaction().commit();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a1 = s.get(A.class, 1L);
|
||||
System.out.println("here1");
|
||||
assertThat(a1.bs).hasSize(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
Assert.assertEquals(0, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a2 = s.get(A.class, 1L);
|
||||
B b2 = a2.bs.iterator().next();
|
||||
assertThat(a2.bs.size()).isEqualTo(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
s.close();
|
||||
|
||||
Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
stats.logSummary();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
},
|
||||
"registrationKey": "LJk6V4gqOgTOKZ53Cf2MywSb3/M+AW4igUgBG0pLbj77G2AMBpBkELbiuT9B3HSwqhGN5kxDNtPeUpLdTJLdNaoWSKLO6P9wozkdens3nfi7VE8VEwArQ1naIBLuGMQ5DZ3AHJ4cvYnj0RkAE/lx6sU/uWzBTB1e9jW9sLDSD8M="
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
{
|
||||
"singleServerConfig":{
|
||||
"address": "redis://127.0.0.1:6379"
|
||||
},
|
||||
"codec":{
|
||||
"class":"org.redisson.codec.FstCodec"
|
||||
},
|
||||
"registrationKey": "LJk6V4gqOgTOKZ53Cf2MywSb3/M+AW4igUgBG0pLbj77G2AMBpBkELbiuT9B3HSwqhGN5kxDNtPeUpLdTJLdNaoWSKLO6P9wozkdens3nfi7VE8VEwArQ1naIBLuGMQ5DZ3AHJ4cvYnj0RkAE/lx6sU/uWzBTB1e9jW9sLDSD8M="
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright (c) 2013-2020 Nikita Koksharov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.hibernate.PropertyNotFoundException;
|
||||
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.persister.collection.CollectionPersister;
|
||||
import org.redisson.client.codec.Codec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class RedissonCacheKeysFactory extends DefaultCacheKeysFactory {
|
||||
|
||||
private final Codec codec;
|
||||
|
||||
public RedissonCacheKeysFactory(Codec codec) {
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
|
||||
try {
|
||||
String[] parts = persister.getRole().split("\\.");
|
||||
Field f = ReflectHelper.findField(id.getClass(), parts[parts.length - 1]);
|
||||
|
||||
Object prev = f.get(id);
|
||||
f.set(id, null);
|
||||
ByteBuf state = codec.getMapKeyEncoder().encode(id);
|
||||
Object newId = codec.getMapKeyDecoder().decode(state, null);
|
||||
state.release();
|
||||
f.set(id, prev);
|
||||
return super.createCollectionKey(newId, persister, factory, tenantIdentifier);
|
||||
} catch (PropertyNotFoundException e) {
|
||||
return super.createCollectionKey(id, persister, factory, tenantIdentifier);
|
||||
} catch (IllegalAccessException | IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package org.redisson.hibernate;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.stat.Statistics;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nikita Koksharov
|
||||
*
|
||||
*/
|
||||
public class CollectionTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class A implements Serializable {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Column(unique = true) String uniqueField;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(
|
||||
name = "a_b",
|
||||
joinColumns = @JoinColumn(
|
||||
name = "unique_field", referencedColumnName = "uniqueField"),
|
||||
inverseJoinColumns = @JoinColumn(
|
||||
name = "b_id", referencedColumnName = "id"))
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
private List<B> bs = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
|
||||
public static class B implements Serializable {
|
||||
@Id Long id;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { A.class, B.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration cfg) {
|
||||
super.configure(cfg);
|
||||
cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
|
||||
cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
|
||||
cfg.setProperty(Environment.USER, "sa");
|
||||
cfg.setProperty(Environment.PASS, "");
|
||||
cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
|
||||
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
|
||||
|
||||
cfg.setProperty(Environment.SHOW_SQL, "true");
|
||||
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
|
||||
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
|
||||
cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
sessionFactory().getCache().evictEntityRegions();
|
||||
sessionFactory().getStatistics().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuery() {
|
||||
Statistics stats = sessionFactory().getStatistics();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
A a = new A();
|
||||
a.id = 1L;
|
||||
a.uniqueField = "1";
|
||||
B b = new B();
|
||||
b.id = 1L;
|
||||
s.save(b);
|
||||
a.bs.add(b);
|
||||
s.save(a);
|
||||
s.flush();
|
||||
s.getTransaction().commit();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a1 = s.get(A.class, 1L);
|
||||
System.out.println("here1");
|
||||
assertThat(a1.bs).hasSize(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
Assert.assertEquals(0, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
A a2 = s.get(A.class, 1L);
|
||||
B b2 = a2.bs.iterator().next();
|
||||
assertThat(a2.bs.size()).isEqualTo(1);
|
||||
s.getTransaction().commit();
|
||||
|
||||
s.close();
|
||||
|
||||
Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("org.redisson.hibernate.CollectionTest$A.bs").getHitCount());
|
||||
|
||||
stats.logSummary();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue