|
| 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 org.hibernate.cfg.AvailableSettings; |
| 10 | +import org.hibernate.stat.spi.StatisticsImplementor; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.domain.gambit.BasicEntity; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.Jira; |
| 15 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.hibernate.testing.orm.junit.Setting; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.BeforeAll; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Marco Belladelli |
| 27 | + */ |
| 28 | +@DomainModel( annotatedClasses = { |
| 29 | + BasicEntity.class |
| 30 | +} ) |
| 31 | +@SessionFactory( generateStatistics = true ) |
| 32 | +@ServiceRegistry( settings = { |
| 33 | + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "true" ), |
| 34 | + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ) |
| 35 | +} ) |
| 36 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18439" ) |
| 37 | +public class QueryCacheNullValueTest { |
| 38 | + @Test |
| 39 | + public void testNullProperty(SessionFactoryScope scope) { |
| 40 | + executeQuery( scope, "select data from BasicEntity" ); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testNullLiteral(SessionFactoryScope scope) { |
| 45 | + executeQuery( scope, "select null from BasicEntity" ); |
| 46 | + } |
| 47 | + |
| 48 | + private static void executeQuery(SessionFactoryScope scope, String hql) { |
| 49 | + scope.getSessionFactory().getCache().evictQueryRegions(); |
| 50 | + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); |
| 51 | + statistics.clear(); |
| 52 | + |
| 53 | + for ( int i = 0; i < 2; i++ ) { |
| 54 | + final int hitCount = i; |
| 55 | + scope.inTransaction( session -> { |
| 56 | + assertThat( session.createQuery( hql, String.class ) |
| 57 | + .setCacheable( true ) |
| 58 | + .getSingleResult() ).isNull(); |
| 59 | + // 0 hits, 1 miss, 1 put |
| 60 | + assertThat( statistics.getQueryCacheHitCount() ).isEqualTo( hitCount ); |
| 61 | + assertThat( statistics.getQueryCacheMissCount() ).isEqualTo( 1 ); |
| 62 | + assertThat( statistics.getQueryCachePutCount() ).isEqualTo( 1 ); |
| 63 | + session.clear(); |
| 64 | + } ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeAll |
| 69 | + public void setUp(SessionFactoryScope scope) { |
| 70 | + scope.inTransaction( session -> session.persist( new BasicEntity( 1, null ) ) ); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterAll |
| 74 | + public void tearDown(SessionFactoryScope scope) { |
| 75 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 76 | + } |
| 77 | +} |
0 commit comments