|
| 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.querycache; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | + |
| 11 | +import org.hibernate.CacheMode; |
| 12 | +import org.hibernate.cache.internal.BasicCacheKeyImplementation; |
| 13 | +import org.hibernate.cache.spi.CacheImplementor; |
| 14 | +import org.hibernate.cache.spi.access.CollectionDataAccess; |
| 15 | +import org.hibernate.cache.spi.entry.CollectionCacheEntry; |
| 16 | +import org.hibernate.cache.spi.support.AbstractReadWriteAccess; |
| 17 | +import org.hibernate.cache.spi.support.CollectionReadWriteAccess; |
| 18 | +import org.hibernate.cache.spi.support.DomainDataStorageAccess; |
| 19 | +import org.hibernate.cfg.AvailableSettings; |
| 20 | +import org.hibernate.metamodel.model.domain.NavigableRole; |
| 21 | + |
| 22 | +import org.hibernate.testing.cache.MapStorageAccessImpl; |
| 23 | +import org.hibernate.testing.orm.domain.StandardDomainModel; |
| 24 | +import org.hibernate.testing.orm.domain.library.Book; |
| 25 | +import org.hibernate.testing.orm.domain.library.Person; |
| 26 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 27 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 28 | +import org.hibernate.testing.orm.junit.Jira; |
| 29 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 30 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 31 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 32 | +import org.hibernate.testing.orm.junit.Setting; |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +import jakarta.persistence.CacheStoreMode; |
| 38 | +import jakarta.persistence.SharedCacheMode; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.fail; |
| 42 | + |
| 43 | +/** |
| 44 | + * Assertions that collections which are join fetched and restricted in a query do not get put into the |
| 45 | + * second level cache with the filtered state |
| 46 | + * |
| 47 | + * @author Steve Ebersole |
| 48 | + */ |
| 49 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 50 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-2003" ) |
| 51 | +@ServiceRegistry(settings = { |
| 52 | + @Setting( name= AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), |
| 53 | + @Setting( name= AvailableSettings.USE_QUERY_CACHE, value = "true"), |
| 54 | +}) |
| 55 | +@DomainModel(standardModels = StandardDomainModel.LIBRARY, sharedCacheMode = SharedCacheMode.ALL) |
| 56 | +@SessionFactory |
| 57 | +public class QueryRestrictedCollectionCachingTests { |
| 58 | + public static final String AUTHORS_ROLE = Book.class.getName() + ".authors"; |
| 59 | + |
| 60 | + @Test |
| 61 | + void testSimpleFetch(SessionFactoryScope sessions) { |
| 62 | + final CacheImplementor cache = sessions.getSessionFactory().getCache(); |
| 63 | + cache.evictAllRegions(); |
| 64 | + |
| 65 | + sessions.inTransaction( (session) -> { |
| 66 | + final Book book = session.createSelectionQuery( "from Book b left join fetch b.authors a", Book.class ).getSingleResult(); |
| 67 | + assertThat( book ).isNotNull(); |
| 68 | + assertThat( book.getAuthors() ).hasSize( 2 ); |
| 69 | + } ); |
| 70 | + |
| 71 | + assertThat( cache.containsCollection( AUTHORS_ROLE, 1 ) ).isTrue(); |
| 72 | + assertThat( extractCachedCollectionKeys( cache, AUTHORS_ROLE, 1 ) ).hasSize( 2 ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testSimpleFetch2(SessionFactoryScope sessions) { |
| 77 | + final CacheImplementor cache = sessions.getSessionFactory().getCache(); |
| 78 | + cache.evictAllRegions(); |
| 79 | + |
| 80 | + sessions.inTransaction( (session) -> { |
| 81 | + final Book book = session.createSelectionQuery( |
| 82 | + "from Book b left join fetch b.authors a where b.id = 1", |
| 83 | + Book.class |
| 84 | + ).getSingleResult(); |
| 85 | + assertThat( book ).isNotNull(); |
| 86 | + assertThat( book.getAuthors() ).hasSize( 2 ); |
| 87 | + } ); |
| 88 | + |
| 89 | + assertThat( cache.containsCollection( AUTHORS_ROLE, 1 ) ).isTrue(); |
| 90 | + assertThat( extractCachedCollectionKeys( cache, AUTHORS_ROLE, 1 ) ).hasSize( 2 ); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testRestrictedFetchWithCacheIgnored(SessionFactoryScope sessions) { |
| 95 | + final CacheImplementor cache = sessions.getSessionFactory().getCache(); |
| 96 | + cache.evictAllRegions(); |
| 97 | + |
| 98 | + sessions.inTransaction( (session) -> { |
| 99 | + final Book book = session |
| 100 | + .createSelectionQuery( "from Book b left join fetch b.authors a where a.id = 1", Book.class ) |
| 101 | + .setCacheMode( CacheMode.IGNORE ) |
| 102 | + .getSingleResult(); |
| 103 | + assertThat( book ).isNotNull(); |
| 104 | + assertThat( book.getAuthors() ).hasSize( 1 ); |
| 105 | + } ); |
| 106 | + |
| 107 | + // we ignored the cache explicitly |
| 108 | + assertThat( cache.containsCollection( AUTHORS_ROLE, 1 ) ).isFalse(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + @FailureExpected |
| 113 | + void testRestrictedFetch(SessionFactoryScope sessions) { |
| 114 | + final CacheImplementor cache = sessions.getSessionFactory().getCache(); |
| 115 | + cache.evictAllRegions(); |
| 116 | + |
| 117 | + sessions.inTransaction( (session) -> { |
| 118 | + final Book book = session |
| 119 | + .createSelectionQuery( "from Book b left join fetch b.authors a where a.id = 1", Book.class ) |
| 120 | + .getSingleResult(); |
| 121 | + assertThat( book ).isNotNull(); |
| 122 | + assertThat( book.getAuthors() ).hasSize( 1 ); |
| 123 | + } ); |
| 124 | + |
| 125 | + // This is the crux of HHH-2003. |
| 126 | + // At the moment we put the filtered collection into the cache |
| 127 | + assertThat( cache.containsCollection( AUTHORS_ROLE, 1 ) ).isTrue(); |
| 128 | + // this is just some deeper checks to show that the data is "corrupt" |
| 129 | + assertThat( extractCachedCollectionKeys( cache, AUTHORS_ROLE, 1 ) ).hasSize( 1 ); |
| 130 | + |
| 131 | + fail( "Really, HHH-2003 the collection to not be cached here" ); |
| 132 | + } |
| 133 | + |
| 134 | + private static Serializable[] extractCachedCollectionKeys(CacheImplementor cache, String role, Integer ownerKey) { |
| 135 | + final NavigableRole navigableRole = new NavigableRole( role ); |
| 136 | + final CollectionReadWriteAccess authorsRegionAccess = (CollectionReadWriteAccess) cache.getCollectionRegionAccess( navigableRole ); |
| 137 | + |
| 138 | + final MapStorageAccessImpl storageAccess = (MapStorageAccessImpl) authorsRegionAccess.getStorageAccess(); |
| 139 | + final BasicCacheKeyImplementation cacheKey = new BasicCacheKeyImplementation( ownerKey, role, ownerKey ); |
| 140 | + final AbstractReadWriteAccess.Item cacheItem = (AbstractReadWriteAccess.Item) storageAccess.getFromData( cacheKey ); |
| 141 | + assertThat( cacheItem ).isNotNull(); |
| 142 | + |
| 143 | + final CollectionCacheEntry cacheEntry = (CollectionCacheEntry) cacheItem.getValue(); |
| 144 | + return cacheEntry.getState(); |
| 145 | + } |
| 146 | + |
| 147 | + @BeforeEach |
| 148 | + void createTestData(SessionFactoryScope sessions) { |
| 149 | + sessions.inTransaction( (session) -> { |
| 150 | + final Person poe = new Person( 1, "John Poe" ); |
| 151 | + session.persist( poe ); |
| 152 | + |
| 153 | + final Person schmidt = new Person( 2, "Jacob Schmidt" ); |
| 154 | + session.persist( schmidt ); |
| 155 | + |
| 156 | + final Person king = new Person( 3, "David King" ); |
| 157 | + session.persist( king ); |
| 158 | + |
| 159 | + final Book nightsEdge = new Book( 1, "A Night's Edge" ); |
| 160 | + nightsEdge.addAuthor( poe ); |
| 161 | + nightsEdge.addAuthor( king ); |
| 162 | + nightsEdge.addEditor( schmidt ); |
| 163 | + session.persist( nightsEdge ); |
| 164 | + } ); |
| 165 | + } |
| 166 | + |
| 167 | + @AfterEach |
| 168 | + void dropTestData(SessionFactoryScope sessions) { |
| 169 | + sessions.inTransaction( (session) -> { |
| 170 | +// session.createNativeMutationQuery( "delete book_authors" ).executeUpdate(); |
| 171 | +// session.createNativeMutationQuery( "delete book_editors" ).executeUpdate(); |
| 172 | + session.createMutationQuery( "delete Book" ).executeUpdate(); |
| 173 | + session.createMutationQuery( "delete Person" ).executeUpdate(); |
| 174 | + } ); |
| 175 | + } |
| 176 | +} |
0 commit comments