|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.query; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.JoinTable; |
| 24 | +import jakarta.persistence.ManyToMany; |
| 25 | +import jakarta.persistence.Tuple; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Marco Belladelli |
| 31 | + */ |
| 32 | +@DomainModel( annotatedClasses = { |
| 33 | + ManyToManyGroupByOrderByTest.Person.class, |
| 34 | + ManyToManyGroupByOrderByTest.Cat.class, |
| 35 | +} ) |
| 36 | +@SessionFactory |
| 37 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17837" ) |
| 38 | +public class ManyToManyGroupByOrderByTest { |
| 39 | + @Test |
| 40 | + public void testSelectEntity(SessionFactoryScope scope) { |
| 41 | + // explicit join group by |
| 42 | + scope.inTransaction( session -> { |
| 43 | + final Person result = session.createQuery( |
| 44 | + "select owner from Cat cat join cat.owners owner group by owner", |
| 45 | + Person.class |
| 46 | + ).getSingleResult(); |
| 47 | + assertThat( result.getId() ).isEqualTo( 1L ); |
| 48 | + } ); |
| 49 | + // explicit join group by + order by |
| 50 | + scope.inTransaction( session -> { |
| 51 | + final Person result = session.createQuery( |
| 52 | + "select owner from Cat cat join cat.owners owner group by owner order by owner", |
| 53 | + Person.class |
| 54 | + ).getSingleResult(); |
| 55 | + assertThat( result.getId() ).isEqualTo( 1L ); |
| 56 | + } ); |
| 57 | + // implicit join group by |
| 58 | + scope.inTransaction( session -> { |
| 59 | + final Person result = session.createQuery( |
| 60 | + "select element(cat.owners) from Cat cat group by element(cat.owners)", |
| 61 | + Person.class |
| 62 | + ).getSingleResult(); |
| 63 | + assertThat( result.getId() ).isEqualTo( 1L ); |
| 64 | + } ); |
| 65 | + // implicit join group by + order by |
| 66 | + scope.inTransaction( session -> { |
| 67 | + final Person result = session.createQuery( |
| 68 | + "select element(cat.owners) from Cat cat group by element(cat.owners) order by element(cat.owners)", |
| 69 | + Person.class |
| 70 | + ).getSingleResult(); |
| 71 | + assertThat( result.getId() ).isEqualTo( 1L ); |
| 72 | + } ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testSelectAssociationId(SessionFactoryScope scope) { |
| 77 | + // explicit join group by |
| 78 | + scope.inTransaction( session -> { |
| 79 | + final Tuple result = session.createQuery( |
| 80 | + "select owner.id, owner.name from Cat cat join cat.owners owner group by owner", |
| 81 | + Tuple.class |
| 82 | + ).getSingleResult(); |
| 83 | + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); |
| 84 | + assertThat( result.get( 1, String.class ) ).isEqualTo( "Marco" ); |
| 85 | + } ); |
| 86 | + // explicit join group by + order by |
| 87 | + scope.inTransaction( session -> { |
| 88 | + final Tuple result = session.createQuery( |
| 89 | + "select owner.id, owner.name from Cat cat join cat.owners owner group by owner order by owner", |
| 90 | + Tuple.class |
| 91 | + ).getSingleResult(); |
| 92 | + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); |
| 93 | + assertThat( result.get( 1, String.class ) ).isEqualTo( "Marco" ); |
| 94 | + } ); |
| 95 | + // implicit join group by |
| 96 | + scope.inTransaction( session -> { |
| 97 | + final Tuple result = session.createQuery( |
| 98 | + "select element(cat.owners).id from Cat cat group by element(cat.owners)", |
| 99 | + Tuple.class |
| 100 | + ).getSingleResult(); |
| 101 | + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); |
| 102 | + } ); |
| 103 | + // implicit join group by + order by |
| 104 | + scope.inTransaction( session -> { |
| 105 | + final Tuple result = session.createQuery( |
| 106 | + "select element(cat.owners).id from Cat cat group by element(cat.owners) order by element(cat.owners)", |
| 107 | + Tuple.class |
| 108 | + ).getSingleResult(); |
| 109 | + assertThat( result.get( 0, Long.class ) ).isEqualTo( 1L ); |
| 110 | + } ); |
| 111 | + } |
| 112 | + |
| 113 | + @BeforeAll |
| 114 | + public void setUp(SessionFactoryScope scope) { |
| 115 | + scope.inTransaction( session -> { |
| 116 | + final Cat cat = new Cat(); |
| 117 | + final Person owner = new Person( 1L, "Marco" ); |
| 118 | + cat.addToOwners( owner ); |
| 119 | + session.persist( owner ); |
| 120 | + session.persist( cat ); |
| 121 | + } ); |
| 122 | + } |
| 123 | + |
| 124 | + @AfterAll |
| 125 | + public void tearDown(SessionFactoryScope scope) { |
| 126 | + scope.inTransaction( session -> { |
| 127 | + session.createMutationQuery( "delete from Cat" ).executeUpdate(); |
| 128 | + session.createMutationQuery( "delete from Person" ).executeUpdate(); |
| 129 | + } ); |
| 130 | + } |
| 131 | + |
| 132 | + @Entity( name = "Person" ) |
| 133 | + static class Person { |
| 134 | + @Id |
| 135 | + private Long id; |
| 136 | + |
| 137 | + private String name; |
| 138 | + |
| 139 | + public Person() { |
| 140 | + } |
| 141 | + |
| 142 | + public Person(Long id, String name) { |
| 143 | + this.id = id; |
| 144 | + this.name = name; |
| 145 | + } |
| 146 | + |
| 147 | + @ManyToMany( mappedBy = "owners" ) |
| 148 | + protected Set<Cat> ownedCats = new HashSet<>(); |
| 149 | + |
| 150 | + public Long getId() { |
| 151 | + return id; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Entity( name = "Cat" ) |
| 156 | + static class Cat { |
| 157 | + @Id |
| 158 | + @GeneratedValue |
| 159 | + protected Long id; |
| 160 | + |
| 161 | + @JoinTable |
| 162 | + @ManyToMany |
| 163 | + private Set<Person> owners = new HashSet<Person>(); |
| 164 | + |
| 165 | + public Set<Person> getOwners() { |
| 166 | + return this.owners; |
| 167 | + } |
| 168 | + |
| 169 | + public void addToOwners(Person person) { |
| 170 | + owners.add( person ); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments