|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.cache; |
| 6 | + |
| 7 | +import org.hibernate.CacheMode; |
| 8 | +import org.hibernate.Session; |
| 9 | +import org.hibernate.annotations.Cache; |
| 10 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 11 | +import org.hibernate.cfg.AvailableSettings; |
| 12 | + |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 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.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import jakarta.persistence.Column; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +@DomainModel(annotatedClasses = { |
| 30 | + QueryCacheIncompleteTest.Admin.class, |
| 31 | +}) |
| 32 | +@SessionFactory |
| 33 | +@ServiceRegistry( |
| 34 | + settings = { |
| 35 | + @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), |
| 36 | + @Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"), |
| 37 | + @Setting(name = AvailableSettings.QUERY_CACHE_LAYOUT, value = "FULL") |
| 38 | + } |
| 39 | +) |
| 40 | +@JiraKey(value = "HHH-18689") |
| 41 | +public class QueryCacheIncompleteTest { |
| 42 | + |
| 43 | + private Long adminId; |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + public void setUp(SessionFactoryScope scope) { |
| 47 | + adminId = scope.fromTransaction( |
| 48 | + session -> { |
| 49 | + Admin admin = new Admin(); |
| 50 | + admin.setAge( 42 ); |
| 51 | + session.persist( admin ); |
| 52 | + return admin.getId(); |
| 53 | + } |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testQueryWithEmbeddableParameter(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( |
| 60 | + session -> { |
| 61 | + // load uninitialized proxy |
| 62 | + session.getReference( Admin.class, adminId ); |
| 63 | + // load entity |
| 64 | + var multiLoader = session.byMultipleIds( Admin.class ); |
| 65 | + multiLoader.with( CacheMode.NORMAL ); |
| 66 | + multiLoader.multiLoad( adminId ); |
| 67 | + |
| 68 | + // store in query cache |
| 69 | + Admin admin = queryAdmin( session ); |
| 70 | + assertThat( admin.getAge() ).isEqualTo( 42 ); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + scope.inTransaction( |
| 75 | + session -> { |
| 76 | + // use query cache |
| 77 | + Admin admin = queryAdmin( session ); |
| 78 | + assertThat( admin.getAge() ).isEqualTo( 42 ); |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + private Admin queryAdmin(Session s) { |
| 84 | + return s.createQuery( "from Admin", Admin.class ).setCacheable( true ).getSingleResult(); |
| 85 | + } |
| 86 | + |
| 87 | + @Entity(name = "Admin") |
| 88 | + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) |
| 89 | + public static class Admin { |
| 90 | + |
| 91 | + @Id |
| 92 | + @GeneratedValue |
| 93 | + private Long id; |
| 94 | + |
| 95 | + @Column(nullable = false) |
| 96 | + private int age; |
| 97 | + |
| 98 | + public Long getId() { |
| 99 | + return id; |
| 100 | + } |
| 101 | + |
| 102 | + public void setId(Long id) { |
| 103 | + this.id = id; |
| 104 | + } |
| 105 | + |
| 106 | + public int getAge() { |
| 107 | + return age; |
| 108 | + } |
| 109 | + |
| 110 | + public void setAge(int age) { |
| 111 | + this.age = age; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments