diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/array/ArrayTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/array/ArrayTests.java index 493f5815d5..7337e625ee 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/array/ArrayTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/array/ArrayTests.java @@ -8,6 +8,7 @@ import javax.persistence.OrderColumn; import javax.persistence.Table; import org.hibernate.query.spi.QueryImplementor; +import org.hibernate.stat.spi.StatisticsImplementor; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.ServiceRegistry; @@ -25,11 +26,13 @@ import static org.hamcrest.MatcherAssert.assertThat; annotatedClasses = { ArrayTests.Employee.class } ) @ServiceRegistry -@SessionFactory +@SessionFactory(generateStatistics = true) public class ArrayTests { @Test public void basicTest(SessionFactoryScope scope) { + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + statistics.clear(); scope.inTransaction( session -> { final QueryImplementor query = session.createQuery( @@ -37,6 +40,7 @@ public class ArrayTests { Employee.class ); final Employee result = query.uniqueResult(); + assertThat( statistics.getPrepareStatementCount(), is(2L) ); assertThat( result, notNullValue() ); assertThat( result.getName(), is( "Koen" ) ); String[] todo = result.getToDoList(); @@ -57,6 +61,8 @@ public class ArrayTests { } ); + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + statistics.clear(); scope.inTransaction( session -> { final QueryImplementor query = session.createQuery( @@ -64,6 +70,7 @@ public class ArrayTests { Employee.class ); final Employee result = query.setParameter( "id", 2 ).uniqueResult(); + assertThat( statistics.getPrepareStatementCount(), is(2L) ); assertThat( result, notNullValue() ); assertThat( result.getName(), is( "Andrea" ) ); @@ -72,6 +79,7 @@ public class ArrayTests { } ); + statistics.clear(); scope.inTransaction( session -> { final QueryImplementor query = session.createQuery( @@ -79,6 +87,7 @@ public class ArrayTests { Employee.class ); final List results = query.list(); + assertThat( statistics.getPrepareStatementCount(), is(3L) ); assertThat( results.size(), is( 2 ) ); results.forEach( employee -> { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyEmptyCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyEmptyCollectionTest.java new file mode 100644 index 0000000000..cac2e6e63f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyEmptyCollectionTest.java @@ -0,0 +1,136 @@ +/* + * 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.onetomany; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.collection.internal.PersistentBag; +import org.hibernate.stat.spi.StatisticsImplementor; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Andrea Boriero + */ +@DomainModel( + annotatedClasses = { + OneToManyEmptyCollectionTest.Order.class, + OneToManyEmptyCollectionTest.Item.class + } +) +@SessionFactory(generateStatistics = true) +public class OneToManyEmptyCollectionTest { + + @Test + public void testHqlSelect(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Order order = new Order(); + session.save( order ); + } + ); + + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + statistics.clear(); + scope.inTransaction( + session -> { + Order order = session.createQuery( "from Order", Order.class ).uniqueResult(); + assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + List items = order.getEagerItems(); + assertThat( items.size(), is( 0 ) ); + assertThat( items, instanceOf( PersistentBag.class ) ); + assertThat( items, instanceOf( PersistentBag.class ) ); + + PersistentBag bag = (PersistentBag) items; + assertThat( bag.getOwner(), sameInstance( order ) ); + assertThat( bag.getKey(), is( 1L ) ); + assertThat( + bag.getRole(), + is( Order.class.getName() + ".eagerItems" ) + ); + assertTrue( bag.wasInitialized() ); + assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + + + List lazyItems = order.getLazyItems(); + assertThat( lazyItems, instanceOf( PersistentBag.class ) ); + assertThat( lazyItems, instanceOf( PersistentBag.class ) ); + + PersistentBag lazyBag = (PersistentBag) lazyItems; + assertThat( lazyBag.getOwner(), sameInstance( order ) ); + assertThat( lazyBag.getKey(), is( 1L ) ); + assertThat( + lazyBag.getRole(), + is( Order.class.getName() + ".lazyItems" ) + ); + assertFalse( lazyBag.wasInitialized() ); + assertThat( statistics.getPrepareStatementCount(), is( 2L ) ); + assertThat( lazyItems.size(), is( 0 ) ); + assertThat( statistics.getPrepareStatementCount(), is( 3L ) ); + } + ); + } + + @Entity(name = "Order") + @Table(name = "`ORDER`") + public static class Order { + @Id + @GeneratedValue + private Long id; + + @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) + private List eagerItems = new ArrayList<>(); + + @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) + private List lazyItems = new ArrayList<>(); + + public Order() { + } + + public List getEagerItems() { + return eagerItems; + } + + public List getLazyItems() { + return lazyItems; + } + + public void setLazyItems(List lazyItems) { + this.lazyItems = lazyItems; + } + } + + @Entity(name = "Item") + public static class Item { + @Id + @GeneratedValue + private Long id; + + private String name; + + @ManyToOne + private Order order; + } +}