From d1675997932ac9682af217735b6c9f3ea69b524c Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 4 Nov 2015 21:43:52 -0600 Subject: [PATCH] HHH-9481 - SQL query clears all cache (cherry picked from commit 9128b84b54f64140c6dbd53d7be10371c93869cd) --- .../NativeQuerySyncSpaceCachingTest.java | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/sql/syncSpace/NativeQuerySyncSpaceCachingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/sql/syncSpace/NativeQuerySyncSpaceCachingTest.java b/hibernate-core/src/test/java/org/hibernate/test/sql/syncSpace/NativeQuerySyncSpaceCachingTest.java new file mode 100644 index 0000000000..c86248dc07 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/sql/syncSpace/NativeQuerySyncSpaceCachingTest.java @@ -0,0 +1,201 @@ +/* + * 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 . + */ +package org.hibernate.test.sql.syncSpace; + +import java.util.Map; + +import javax.persistence.Cacheable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.SQLQuery; +import org.hibernate.Session; +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Tests of how sync-spaces for a native query affect caching + * + * @author Samuel Fung + * @author Steve Ebersole + */ +public class NativeQuerySyncSpaceCachingTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected void addSettings(Map settings) { + super.addSettings( settings ); + settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Customer.class, Address.class }; + } + + @Before + public void before() { + Session session = sessionFactory().openSession(); + session.beginTransaction(); + Customer customer = new Customer( 1, "Samuel" ); + session.saveOrUpdate( customer ); + session.getTransaction().commit(); + session.close(); + } + + @After + public void after() { + Session session = sessionFactory().openSession(); + session.beginTransaction(); + session.createQuery( "delete Customer" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testSelectAnotherEntityWithNoSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "select * from Address" ).list(); + session.close(); + + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Test + public void testUpdateAnotherEntityWithNoSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "update Address set id = id" ).executeUpdate(); + session.close(); + + // NOTE false here because executeUpdate is different than selects + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Test + public void testUpdateAnotherEntityWithSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "update Address set id = id" ).addSynchronizedEntityClass( Address.class ).executeUpdate(); + session.close(); + + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Test + public void testSelectCachedEntityWithNoSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "select * from Customer" ).list(); + session.close(); + + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Test + public void testUpdateCachedEntityWithNoSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "update Customer set id = id" ).executeUpdate(); + session.close(); + + // NOTE false here because executeUpdate is different than selects + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Test + public void testUpdateCachedEntityWithSyncSpaces() { + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + + Session session = openSession(); + session.createSQLQuery( "update Customer set id = id" ).addSynchronizedEntityClass( Customer.class ).executeUpdate(); + session.close(); + + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); + } + + @Entity( name = "Customer" ) + @Table(name="Customer") + @Cacheable + @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) + public static class Customer { + @Id + private int id; + + private String name; + + public Customer() { + } + + public Customer(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity( name = "Address" ) + @Table(name="Address") + public static class Address { + @Id + private int id; + private String text; + + public Address() { + } + + public Address(int id, String text) { + this.id = id; + this.text = text; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + } +}