HHH-15485 Tests for Collections in default FetchGroup are not enabling this property

This commit is contained in:
Andrea Boriero 2022-09-07 11:28:47 +02:00 committed by Christian Beikov
parent 92d332055b
commit 76c67f080c
6 changed files with 401 additions and 18 deletions

View File

@ -20,37 +20,43 @@ import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
/**
* @author Christian Beikov
*/
@TestForIssue( jiraKey = "HHH-14348" )
@RunWith( BytecodeEnhancerRunner.class )
public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseCoreFunctionalTestCase {
public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{StringsEntity.class};
}
@Before
public void checkSettings() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( true );
}
@Before
public void prepare() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
doInJPA( this::sessionFactory, em -> {
StringsEntity entity = new StringsEntity();
entity.id = 1L;
@ -62,8 +68,17 @@ public class DirtyTrackingCollectionInDefaultFetchGroupTest extends BaseCoreFunc
@Test
public void test() {
doInJPA( this::sessionFactory, entityManager -> {
StringsEntity entity = entityManager.find( StringsEntity.class, 1L );
entityManager.flush();
BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) ( (PersistentAttributeInterceptable) entity )
.$$_hibernate_getInterceptor();
// the attributes are initialized with a PersistentCollection that is not initialized
assertFalse( interceptor.hasAnyUninitializedAttributes() );
assertTrue( interceptor.isAttributeLoaded( "someStrings" ) );
assertTrue( interceptor.isAttributeLoaded( "someStringEntities" ) );
assertFalse( Hibernate.isInitialized( entity.someStrings ) );
assertFalse( Hibernate.isInitialized( entity.someStringEntities ) );
} );

View File

@ -0,0 +1,99 @@
/*
* 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.bytecode.enhancement.dirty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import jakarta.persistence.CascadeType;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Christian Beikov
*/
@TestForIssue( jiraKey = "HHH-14348" )
@RunWith( BytecodeEnhancerRunner.class )
public class DirtyTrackingCollectionNotInDefaultFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{StringsEntity.class};
}
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( false );
}
@Before
public void prepare() {
assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
doInJPA( this::sessionFactory, em -> {
StringsEntity entity = new StringsEntity();
entity.id = 1L;
entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) );
em.persist( entity );
} );
}
@Test
public void test() {
doInJPA( this::sessionFactory, entityManager -> {
StringsEntity entity = entityManager.find( StringsEntity.class, 1L );
entityManager.flush();
BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) ( (PersistentAttributeInterceptable) entity )
.$$_hibernate_getInterceptor();
assertTrue( interceptor.hasAnyUninitializedAttributes() );
assertFalse( interceptor.isAttributeLoaded( "someStrings" ) );
assertFalse( interceptor.isAttributeLoaded( "someStringEntities" ) );
} );
}
// --- //
@Entity
@Table( name = "STRINGS_ENTITY" )
private static class StringsEntity {
@Id
Long id;
@ElementCollection
List<String> someStrings;
@ManyToOne(fetch = FetchType.LAZY)
StringsEntity parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
Set<StringsEntity> someStringEntities;
}
}

View File

@ -0,0 +1,170 @@
/*
* 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.bytecode.enhancement.dirty;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.Basic;
import jakarta.persistence.CascadeType;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
/**
* @author Christian Beikov
*/
@TestForIssue(jiraKey = "HHH-14360")
@RunWith(BytecodeEnhancerRunner.class)
public class DirtyTrackingNotInDefaultFetchGroupPersistTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { HotherEntity.class, Hentity.class };
}
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( false );
}
@Test
public void test() {
assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
Hentity hentity = new Hentity();
HotherEntity hotherEntity = new HotherEntity();
hentity.setLineItems( new ArrayList<>( Collections.singletonList( hotherEntity ) ) );
hentity.setNextRevUNs( new ArrayList<>( Collections.singletonList( "something" ) ) );
doInHibernate( this::sessionFactory, session -> {
session.persist( hentity );
} );
doInHibernate( this::sessionFactory, session -> {
hentity.bumpNumber();
session.saveOrUpdate( hentity );
} );
}
// --- //
@Entity(name = "HotherEntity")
public static class HotherEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Basic
private Long clicId;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public Long getClicId() {
return clicId;
}
public void setClicId(Long clicId) {
this.clicId = clicId;
}
}
@Entity(name = "Hentity")
public static class Hentity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ElementCollection
@OrderColumn(name = "nextRevUN_index")
private List<String> nextRevUNs;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "clicId")
@OrderBy("id asc")
protected List<HotherEntity> lineItems;
@Basic
private Long aNumber;
@Temporal(value = TemporalType.TIMESTAMP)
private Date createDate;
@Temporal(value = TemporalType.TIMESTAMP)
private Date deleteDate;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public List<String> getNextRevUNs() {
return nextRevUNs;
}
public void setNextRevUNs(List<String> nextRevUNs) {
this.nextRevUNs = nextRevUNs;
}
public List<HotherEntity> getLineItems() {
return lineItems;
}
public void setLineItems(List<HotherEntity> lineItems) {
this.lineItems = lineItems;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getDeleteDate() {
return deleteDate;
}
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
public void bumpNumber() {
aNumber = aNumber == null ? 0 : aNumber++;
}
}
}

View File

@ -25,35 +25,39 @@ import jakarta.persistence.OrderColumn;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Before;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
/**
* @author Christian Beikov
*/
@TestForIssue(jiraKey = "HHH-14360")
@RunWith(BytecodeEnhancerRunner.class)
public class DirtyTrackingPersistTest extends BaseCoreFunctionalTestCase {
public class DirtyTrackingPersistTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { HotherEntity.class, Hentity.class };
}
@Before
public void checkSettings() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( true );
}
@Test
public void test() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
Hentity hentity = new Hentity();
HotherEntity hotherEntity = new HotherEntity();
hentity.setLineItems( new ArrayList<>( Collections.singletonList( hotherEntity ) ) );

View File

@ -15,10 +15,13 @@ import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
@ -29,20 +32,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* @author Christian Beikov
*/
@RunWith( BytecodeEnhancerRunner.class )
public class MultipleBagsInLazyFetchGroupTest extends BaseCoreFunctionalTestCase {
public class MultipleBagsInLazyFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{StringsEntity.class};
}
@Before
public void checkSettings() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( true );
}
@Before
public void prepare() {
assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
doInJPA( this::sessionFactory, em -> {
StringsEntity entity = new StringsEntity();
entity.id = 1L;
@ -55,6 +61,7 @@ public class MultipleBagsInLazyFetchGroupTest extends BaseCoreFunctionalTestCase
@Test
public void test() {
Assertions.assertTrue( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
doInJPA( this::sessionFactory, entityManager -> {
StringsEntity entity = entityManager.getReference( StringsEntity.class, 1L );
assertEquals( 3, entity.someStrings.size() );

View File

@ -0,0 +1,88 @@
/*
* 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.bytecode.enhancement.lazy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* @author Christian Beikov
*/
@RunWith( BytecodeEnhancerRunner.class )
public class MultipleBagsNotInLazyFetchGroupTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{StringsEntity.class};
}
@Override
protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) {
super.configureSessionFactoryBuilder( sfb );
sfb.applyCollectionsInDefaultFetchGroup( false );
}
@Before
public void prepare() {
assertFalse( sessionFactory().getSessionFactoryOptions().isCollectionsInDefaultFetchGroupEnabled() );
doInJPA( this::sessionFactory, em -> {
StringsEntity entity = new StringsEntity();
entity.id = 1L;
entity.text = "abc";
entity.someStrings = new ArrayList<>( Arrays.asList( "a", "b", "c" ) );
entity.someStrings2 = new ArrayList<>( Arrays.asList( "a", "b", "c" ) );
em.persist( entity );
} );
}
@Test
public void test() {
doInJPA( this::sessionFactory, entityManager -> {
StringsEntity entity = entityManager.getReference( StringsEntity.class, 1L );
assertEquals( 3, entity.someStrings.size() );
assertEquals( 3, entity.someStrings2.size() );
} );
}
// --- //
@Entity
@Table( name = "STRINGS_ENTITY" )
private static class StringsEntity {
@Id
Long id;
String text;
@ElementCollection(fetch = FetchType.EAGER)
List<String> someStrings;
@ElementCollection(fetch = FetchType.EAGER)
List<String> someStrings2;
}
}