HHH-11766 : test case
HHH-11766 : Renamed and updated test to be more comprehensive; added another test with a lazy basic value
(cherry picked from commit 1c151f956d
)
This commit is contained in:
parent
7ccf3d8a6e
commit
ddfb486f15
|
@ -0,0 +1,166 @@
|
|||
package org.hibernate.test.bytecode.enhancement.lazy.cache;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Cacheable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.AssertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.annotations.LazyToOne;
|
||||
import org.hibernate.annotations.LazyToOneOption;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.stat.CacheRegionStatistics;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.hibernate.testing.transaction.TransactionUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(BytecodeEnhancerRunner.class)
|
||||
public class UninitializedAssociationsInCacheTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
public Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[]{Employee.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
|
||||
configuration.setProperty(AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "false");
|
||||
configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11766")
|
||||
public void attributeLoadingFromCache() {
|
||||
final AtomicLong bossId = new AtomicLong();
|
||||
final AtomicLong teamleaderId = new AtomicLong();
|
||||
final AtomicLong teammemberId = new AtomicLong();
|
||||
|
||||
TransactionUtil.doInHibernate(
|
||||
this::sessionFactory, (s) -> {
|
||||
Employee boss = new Employee();
|
||||
Employee teamleader = new Employee();
|
||||
Employee teammember = new Employee();
|
||||
boss.regularString = "boss";
|
||||
teamleader.regularString = "leader";
|
||||
teammember.regularString = "member";
|
||||
s.persist( boss );
|
||||
s.persist( teamleader );
|
||||
s.persist( teammember );
|
||||
boss.subordinates.add( teamleader );
|
||||
teamleader.superior = boss;
|
||||
teamleader.subordinates.add( teammember );
|
||||
teammember.superior = teamleader;
|
||||
bossId.set( boss.id );
|
||||
teamleaderId.set( teamleader.id );
|
||||
teammemberId.set( teammember.id );
|
||||
}
|
||||
);
|
||||
|
||||
sessionFactory().getCache().evictAll();
|
||||
sessionFactory().getStatistics().clear();
|
||||
CacheRegionStatistics regionStatistics = sessionFactory().getStatistics().getCacheRegionStatistics( "Employee" );
|
||||
|
||||
TransactionUtil.doInHibernate(
|
||||
this::sessionFactory, (s) -> {
|
||||
final Employee boss = s.find( Employee.class, bossId.get() );
|
||||
Assert.assertEquals( "boss", boss.regularString );
|
||||
final Employee leader = s.find( Employee.class, teamleaderId.get() );
|
||||
Assert.assertEquals( "leader", leader.regularString );
|
||||
final Employee member = s.find( Employee.class, teammemberId.get() );
|
||||
Assert.assertEquals( "member", member.regularString );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( boss, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( boss, "subordinates" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( leader, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( leader, "subordinates" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "subordinates" ) );
|
||||
}
|
||||
);
|
||||
|
||||
assertEquals( 0, regionStatistics.getHitCount() );
|
||||
assertEquals( 3, regionStatistics.getMissCount() );
|
||||
assertEquals( 3, regionStatistics.getPutCount() );
|
||||
|
||||
TransactionUtil.doInHibernate(
|
||||
this::sessionFactory, (s) -> {
|
||||
final Employee boss = s.find( Employee.class, bossId.get() );
|
||||
final Employee leader = s.find( Employee.class, teamleaderId.get() );
|
||||
final Employee member = s.find( Employee.class, teammemberId.get() );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( boss, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( boss, "subordinates" ) );
|
||||
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "subordinates" ) );
|
||||
Assert.assertNull( boss.superior );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( boss, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( boss, "subordinates" ) );
|
||||
Assert.assertEquals( leader, boss.subordinates.iterator().next() );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( boss, "subordinates" ) );
|
||||
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( leader, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( leader, "subordinates" ) );
|
||||
Assert.assertEquals( boss, leader.superior );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( leader, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( leader, "subordinates" ) );
|
||||
Assert.assertEquals( member, leader.subordinates.iterator().next() );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( leader, "subordinates" ) );
|
||||
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "subordinates" ) );
|
||||
Assert.assertEquals( leader, member.superior );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( member, "superior" ) );
|
||||
Assert.assertFalse( Hibernate.isPropertyInitialized( member, "subordinates" ) );
|
||||
Assert.assertTrue( member.subordinates.isEmpty() );
|
||||
Assert.assertTrue( Hibernate.isPropertyInitialized( member, "subordinates" ) );
|
||||
}
|
||||
);
|
||||
|
||||
assertEquals( 3, regionStatistics.getHitCount() );
|
||||
assertEquals( 3, regionStatistics.getMissCount() );
|
||||
assertEquals( 3, regionStatistics.getPutCount() );
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "EMPLOYEE_TABLE")
|
||||
@Cacheable
|
||||
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "Employee")
|
||||
private static class Employee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "SUPERIOR")
|
||||
@LazyToOne( value = LazyToOneOption.NO_PROXY )
|
||||
Employee superior;
|
||||
|
||||
@OneToMany(mappedBy = "superior")
|
||||
List<Employee> subordinates = new ArrayList<>();
|
||||
|
||||
@Basic
|
||||
String regularString;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* 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.test.bytecode.enhancement.lazy.cache;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Cacheable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.stat.CacheRegionStatistics;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Aaron Schmischke
|
||||
* @author Gail Badner
|
||||
*/
|
||||
@RunWith( BytecodeEnhancerRunner.class )
|
||||
public class UninitializedLazyBasicCacheTest extends BaseCoreFunctionalTestCase {
|
||||
private Long personId;
|
||||
|
||||
@Override
|
||||
public Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { Person.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
|
||||
configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
this.personId = doInHibernate(
|
||||
this::sessionFactory, s -> {
|
||||
|
||||
final Person person = new Person();
|
||||
person.setLazyAttribute( "does_not_matter" );
|
||||
s.persist( person );
|
||||
return person.getId();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11766")
|
||||
public void test() {
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
sessionFactory().getCache().evictAll();
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory, s -> {
|
||||
final Person person = s.get( Person.class, personId );
|
||||
assertFalse( Hibernate.isPropertyInitialized( person, "lazyAttribute" ) );
|
||||
}
|
||||
);
|
||||
|
||||
CacheRegionStatistics regionStatistics = sessionFactory().getStatistics().getCacheRegionStatistics( "Person" );
|
||||
assertEquals( 0, regionStatistics.getHitCount() );
|
||||
assertEquals( 1, regionStatistics.getMissCount() );
|
||||
assertEquals( 1, regionStatistics.getPutCount() );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory, s -> {
|
||||
final Person person = s.get( Person.class, personId );
|
||||
assertFalse( Hibernate.isPropertyInitialized( person, "lazyAttribute" ) );
|
||||
person.getLazyAttribute();
|
||||
assertTrue( Hibernate.isPropertyInitialized( person, "lazyAttribute" ) );
|
||||
}
|
||||
);
|
||||
assertEquals( 1, regionStatistics.getHitCount() );
|
||||
assertEquals( 1, regionStatistics.getMissCount() );
|
||||
assertEquals( 1, regionStatistics.getPutCount() );
|
||||
}
|
||||
|
||||
@Cacheable
|
||||
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all", region = "Person")
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "lazyAttribute")
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
private String lazyAttribute;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLazyAttribute() {
|
||||
return lazyAttribute;
|
||||
}
|
||||
|
||||
public void setLazyAttribute(String lazyAttribute) {
|
||||
this.lazyAttribute = lazyAttribute;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue