From d0febfd0427e2179cee03a6b9d8fe9321493d199 Mon Sep 17 00:00:00 2001 From: Fabio Massimo Ercoli Date: Tue, 11 May 2021 09:42:19 +0200 Subject: [PATCH] HHH-14585 Test more size() cases In particular projections using a size function --- .../hibernate/test/hql/size/filter/City.java | 60 ++++++++ .../test/hql/size/filter/Region.java | 49 +++++++ .../WhereAnnotatedOneToManySizeTest.java | 133 ++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/City.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/Region.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/WhereAnnotatedOneToManySizeTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/City.java b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/City.java new file mode 100644 index 0000000000..c60400e547 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/City.java @@ -0,0 +1,60 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hql.size.filter; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +import org.hibernate.annotations.Where; + +@Entity +@Where(clause = "deleted = false") +public class City { + + @Id + private Integer id; + + private String name; + + @ManyToOne + private Region region; + + private Boolean deleted; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Region getRegion() { + return region; + } + + public void setRegion(Region region) { + this.region = region; + } + + public Boolean getDeleted() { + return deleted; + } + + public void setDeleted(Boolean deleted) { + this.deleted = deleted; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/Region.java b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/Region.java new file mode 100644 index 0000000000..59da4d4f35 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/Region.java @@ -0,0 +1,49 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hql.size.filter; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class Region { + + @Id + private Integer id; + + private String name; + + @OneToMany(mappedBy = "region") + private List cities = new ArrayList<>(); + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCities() { + return cities; + } + + public void setCities(List cities) { + this.cities = cities; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/WhereAnnotatedOneToManySizeTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/WhereAnnotatedOneToManySizeTest.java new file mode 100644 index 0000000000..a5adb3af5b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/size/filter/WhereAnnotatedOneToManySizeTest.java @@ -0,0 +1,133 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hql.size.filter; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.hibernate.dialect.DB2Dialect; +import org.hibernate.query.spi.QueryImplementor; + +import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +@TestForIssue(jiraKey = "HHH-14585") +public class WhereAnnotatedOneToManySizeTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Region.class, City.class }; + } + + @Before + public void before() { + Region lazio = new Region(); + lazio.setId( 1 ); + lazio.setName( "Lazio" ); + + Region lombardy = new Region(); + lombardy.setId( 2 ); + lombardy.setName( "Lombardy" ); + + City rome = new City(); + rome.setId( 1 ); + rome.setName( "Rome" ); + rome.setDeleted( false ); + rome.setRegion( lazio ); + + City gradoli = new City(); + gradoli.setId( 2 ); + gradoli.setName( "Gradoli" ); + gradoli.setDeleted( true ); + gradoli.setRegion( lazio ); + + City milan = new City(); + milan.setId( 3 ); + milan.setName( "Milan" ); + milan.setDeleted( false ); + milan.setRegion( lombardy ); + + City pavia = new City(); + pavia.setId( 4 ); + pavia.setName( "Pavia" ); + pavia.setDeleted( false ); + pavia.setRegion( lombardy ); + + lazio.getCities().add( rome ); + lazio.getCities().add( gradoli ); + + lombardy.getCities().add( milan ); + lombardy.getCities().add( pavia ); + + inTransaction( session -> { + session.persist( lazio ); + session.persist( lombardy ); + + session.persist( rome ); + session.persist( gradoli ); + session.persist( milan ); + session.persist( pavia ); + } ); + } + + @After + public void after() { + inTransaction( session -> { + session.createQuery( "DELETE FROM City c" ).executeUpdate(); + session.createQuery( "DELETE FROM Region c" ).executeUpdate(); + } ); + } + + @Test + @SkipForDialect(value = DB2Dialect.class, comment = "DB2 does not support correlated subqueries in the ORDER BY clause") + public void orderBy_sizeOf() { + inSession( session -> { + QueryImplementor query = session.createQuery( + "select r, size(r.cities) from Region r order by size(r.cities) desc" ); + List result = query.getResultList(); + assertThat( result ).extracting( f -> f[0] ).extracting( "name" ).containsExactly( "Lombardy", "Lazio" ); + assertThat( result ).extracting( f -> f[1] ).containsExactly( 2, 1 ); + } ); + } + + @Test + @SkipForDialect(value = DB2Dialect.class, comment = "DB2 does not support correlated subqueries in the ORDER BY clause") + public void orderBy_dotSize() { + inSession( session -> { + QueryImplementor query = session.createQuery( + "select r, r.cities.size from Region r order by r.cities.size desc" ); + List result = query.getResultList(); + assertThat( result ).extracting( f -> f[0] ).extracting( "name" ).containsExactly( "Lombardy", "Lazio" ); + assertThat( result ).extracting( f -> f[1] ).containsExactly( 2, 1 ); + } ); + } + + @Test + public void project_sizeOf() { + inSession( session -> { + QueryImplementor query = session.createQuery( + "SELECT size(r.cities) FROM Region r", Integer.class ); + List cityCounts = query.getResultList(); + assertThat( cityCounts ).containsExactlyInAnyOrder( 1, 2 ); + } ); + } + + @Test + public void project_dotSize() { + inSession( session -> { + QueryImplementor query = session.createQuery( + "SELECT r.cities.size FROM Region r", Integer.class ); + List cityCounts = query.getResultList(); + assertThat( cityCounts ).containsExactlyInAnyOrder( 1, 2 ); + } ); + } +}