|
| 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.test.sql.syncSpace; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import javax.persistence.Cacheable; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.Id; |
| 14 | +import javax.persistence.Table; |
| 15 | + |
| 16 | +import org.hibernate.SQLQuery; |
| 17 | +import org.hibernate.Session; |
| 18 | +import org.hibernate.annotations.Cache; |
| 19 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 20 | +import org.hibernate.cfg.AvailableSettings; |
| 21 | + |
| 22 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests of how sync-spaces for a native query affect caching |
| 32 | + * |
| 33 | + * @author Samuel Fung |
| 34 | + * @author Steve Ebersole |
| 35 | + */ |
| 36 | +public class NativeQuerySyncSpaceCachingTest extends BaseNonConfigCoreFunctionalTestCase { |
| 37 | + @Override |
| 38 | + protected void addSettings(Map settings) { |
| 39 | + super.addSettings( settings ); |
| 40 | + settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, true ); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected Class[] getAnnotatedClasses() { |
| 45 | + return new Class[] { Customer.class, Address.class }; |
| 46 | + } |
| 47 | + |
| 48 | + @Before |
| 49 | + public void before() { |
| 50 | + Session session = sessionFactory().openSession(); |
| 51 | + session.beginTransaction(); |
| 52 | + Customer customer = new Customer( 1, "Samuel" ); |
| 53 | + session.saveOrUpdate( customer ); |
| 54 | + session.getTransaction().commit(); |
| 55 | + session.close(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void after() { |
| 60 | + Session session = sessionFactory().openSession(); |
| 61 | + session.beginTransaction(); |
| 62 | + session.createQuery( "delete Customer" ).executeUpdate(); |
| 63 | + session.getTransaction().commit(); |
| 64 | + session.close(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSelectAnotherEntityWithNoSyncSpaces() { |
| 69 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 70 | + |
| 71 | + Session session = openSession(); |
| 72 | + session.createSQLQuery( "select * from Address" ).list(); |
| 73 | + session.close(); |
| 74 | + |
| 75 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testUpdateAnotherEntityWithNoSyncSpaces() { |
| 80 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 81 | + |
| 82 | + Session session = openSession(); |
| 83 | + session.createSQLQuery( "update Address set id = id" ).executeUpdate(); |
| 84 | + session.close(); |
| 85 | + |
| 86 | + // NOTE false here because executeUpdate is different than selects |
| 87 | + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUpdateAnotherEntityWithSyncSpaces() { |
| 92 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 93 | + |
| 94 | + Session session = openSession(); |
| 95 | + session.createSQLQuery( "update Address set id = id" ).addSynchronizedEntityClass( Address.class ).executeUpdate(); |
| 96 | + session.close(); |
| 97 | + |
| 98 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testSelectCachedEntityWithNoSyncSpaces() { |
| 103 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 104 | + |
| 105 | + Session session = openSession(); |
| 106 | + session.createSQLQuery( "select * from Customer" ).list(); |
| 107 | + session.close(); |
| 108 | + |
| 109 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testUpdateCachedEntityWithNoSyncSpaces() { |
| 114 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 115 | + |
| 116 | + Session session = openSession(); |
| 117 | + session.createSQLQuery( "update Customer set id = id" ).executeUpdate(); |
| 118 | + session.close(); |
| 119 | + |
| 120 | + // NOTE false here because executeUpdate is different than selects |
| 121 | + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testUpdateCachedEntityWithSyncSpaces() { |
| 126 | + assertTrue( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 127 | + |
| 128 | + Session session = openSession(); |
| 129 | + session.createSQLQuery( "update Customer set id = id" ).addSynchronizedEntityClass( Customer.class ).executeUpdate(); |
| 130 | + session.close(); |
| 131 | + |
| 132 | + assertFalse( sessionFactory().getCache().containsEntity( Customer.class, 1 ) ); |
| 133 | + } |
| 134 | + |
| 135 | + @Entity( name = "Customer" ) |
| 136 | + @Table(name="Customer") |
| 137 | + @Cacheable |
| 138 | + @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) |
| 139 | + public static class Customer { |
| 140 | + @Id |
| 141 | + private int id; |
| 142 | + |
| 143 | + private String name; |
| 144 | + |
| 145 | + public Customer() { |
| 146 | + } |
| 147 | + |
| 148 | + public Customer(int id, String name) { |
| 149 | + this.id = id; |
| 150 | + this.name = name; |
| 151 | + } |
| 152 | + |
| 153 | + public int getId() { |
| 154 | + return id; |
| 155 | + } |
| 156 | + |
| 157 | + public void setId(int id) { |
| 158 | + this.id = id; |
| 159 | + } |
| 160 | + |
| 161 | + public String getName() { |
| 162 | + return name; |
| 163 | + } |
| 164 | + |
| 165 | + public void setName(String name) { |
| 166 | + this.name = name; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Entity( name = "Address" ) |
| 171 | + @Table(name="Address") |
| 172 | + public static class Address { |
| 173 | + @Id |
| 174 | + private int id; |
| 175 | + private String text; |
| 176 | + |
| 177 | + public Address() { |
| 178 | + } |
| 179 | + |
| 180 | + public Address(int id, String text) { |
| 181 | + this.id = id; |
| 182 | + this.text = text; |
| 183 | + } |
| 184 | + |
| 185 | + public int getId() { |
| 186 | + return id; |
| 187 | + } |
| 188 | + |
| 189 | + public void setId(int id) { |
| 190 | + this.id = id; |
| 191 | + } |
| 192 | + |
| 193 | + public String getText() { |
| 194 | + return text; |
| 195 | + } |
| 196 | + |
| 197 | + public void setText(String text) { |
| 198 | + this.text = text; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments