HHH-10162 - Add test

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2024-05-22 23:00:10 +02:00 committed by Christian Beikov
parent 9da230a616
commit 9c4baed5ae
3 changed files with 82 additions and 2 deletions

View File

@ -0,0 +1,50 @@
/*
* 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 http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.cache.polymorphism;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
/**
*
* @author Christian Beikov
*/
@Entity
public class CacheHolder {
@Id
private String id;
@ManyToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Cacheable item;
public CacheHolder() {
}
public CacheHolder(String id, Cacheable item) {
this.id = id;
this.item = item;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Cacheable getItem() {
return item;
}
public void setItem(Cacheable item) {
this.item = item;
}
}

View File

@ -8,6 +8,7 @@ package org.hibernate.orm.test.cache.polymorphism;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.ConcreteProxy;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@ -18,6 +19,7 @@ import jakarta.persistence.Id;
*/
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@ConcreteProxy
public class Cacheable {
private Long id;
private String name;

View File

@ -26,7 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@JiraKey( "HHH-9028" )
@JiraKey( "HHH-9107" )
@DomainModel( annotatedClasses = { Cacheable.class, CachedItem1.class, CachedItem2.class } )
@DomainModel( annotatedClasses = { Cacheable.class, CachedItem1.class, CachedItem2.class, CacheHolder.class } )
@SessionFactory
public class PolymorphicCacheTest {
@BeforeEach
@ -39,7 +39,12 @@ public class PolymorphicCacheTest {
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete Cacheable" ).executeUpdate() );
scope.inTransaction(
session -> {
session.createMutationQuery("delete from CacheHolder").executeUpdate();
session.createMutationQuery( "delete Cacheable" ).executeUpdate();
}
);
}
@Test
@ -140,4 +145,27 @@ public class PolymorphicCacheTest {
} );
}
@Test
@JiraKey("HHH-10162")
public void testPolymorphismAndCacheWithHolder(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
final CachedItem1 item3 = new CachedItem1(3, "name3");
final CacheHolder holder = new CacheHolder( "holder", item3 );
s.persist( item3 );
s.persist( holder );
}
);
scope.inTransaction(
s -> {
CacheHolder cacheHolder = s.get( CacheHolder.class, "holder" );
Assertions.assertTrue(
cacheHolder.getItem() instanceof CachedItem1,
"Relation was not fetched from L2 cache"
);
}
);
}
}