Add one more test for empty collections
This commit is contained in:
parent
6eb729003a
commit
2fa3889e5a
|
@ -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<Employee> 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<Employee> 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<Employee> query = session.createQuery(
|
||||
|
@ -79,6 +87,7 @@ public class ArrayTests {
|
|||
Employee.class
|
||||
);
|
||||
final List<Employee> results = query.list();
|
||||
assertThat( statistics.getPrepareStatementCount(), is(3L) );
|
||||
|
||||
assertThat( results.size(), is( 2 ) );
|
||||
results.forEach( employee -> {
|
||||
|
|
|
@ -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<Item> 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<Item> 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<Item> eagerItems = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
|
||||
private List<Item> lazyItems = new ArrayList<>();
|
||||
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public List<Item> getEagerItems() {
|
||||
return eagerItems;
|
||||
}
|
||||
|
||||
public List<Item> getLazyItems() {
|
||||
return lazyItems;
|
||||
}
|
||||
|
||||
public void setLazyItems(List<Item> lazyItems) {
|
||||
this.lazyItems = lazyItems;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Item")
|
||||
public static class Item {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
private Order order;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue