HHH-16189 Add test for issue

This commit is contained in:
Andrea Boriero 2023-03-09 16:36:59 +01:00 committed by Christian Beikov
parent f61def53ff
commit fad16b6b1c
3 changed files with 256 additions and 0 deletions

View File

@ -65,6 +65,20 @@ public class MapIssueTest {
); );
} }
@Test
public void testMapKeyDeReferenceDoesNotCauseJoin(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
statementInspector.clear();
scope.inTransaction(
s -> {
s.createQuery( "select c from MapOwner as o left join o.contents c where key(c).id is not null" ).list();
statementInspector.assertExecutedCount( 1 );
// Assert 2 joins, collection table and collection element
statementInspector.assertNumberOfJoins( 0, 2 );
}
);
}
@Test @Test
public void testMapKeyJoinIsReused(SessionFactoryScope scope) { public void testMapKeyJoinIsReused(SessionFactoryScope scope) {
SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();

View File

@ -0,0 +1,137 @@
package org.hibernate.orm.test.orderby;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@DomainModel(
annotatedClasses = {
EmbeddedIdOrderByAndAggregateFunctionTest.Parent.class,
EmbeddedIdOrderByAndAggregateFunctionTest.Child.class,
}
)
@SessionFactory
@TestForIssue(jiraKey = "HHH-16189")
public class EmbeddedIdOrderByAndAggregateFunctionTest {
@Test
@RequiresDialect(PostgreSQLDialect.class)
@RequiresDialect(H2Dialect.class)
public void testSelectWithOrderAndGroupBy(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id,c.name, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"order by c.name",
Object[].class
)
.getResultList();
}
);
}
@Test
public void testSelectWithOrderAndGroupBy2(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"order by c.id",
Object[].class
)
.getResultList();
}
);
}
@Test
public void testSelectWithOrderAndGroupBy3(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id.id1, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id.id1 " +
"order by c.id.id1",
Object[].class
)
.getResultList();
}
);
}
@Test
public void testSelectWithHaving(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"having c.id > :childId",
Object[].class
)
.setParameter( "childId", new ChildId( 1l, 2l ) )
.getResultList();
}
);
}
@Entity(name = "Parent")
public static class Parent {
@Id
private String code;
@ManyToOne
private Child child;
@Column(name = "NUMBER_COLUMN")
int number;
}
@Entity(name = "Child")
public static class Child {
@EmbeddedId
private ChildId id;
private String name;
}
@Embeddable
public static class ChildId {
private long id1;
private long id2;
public ChildId() {
}
public ChildId(long id1, long id2) {
this.id1 = id1;
this.id2 = id2;
}
}
}

View File

@ -0,0 +1,105 @@
package org.hibernate.orm.test.orderby;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@DomainModel(
annotatedClasses = {
OrderByAndAggregateFunctionTest.Parent.class,
OrderByAndAggregateFunctionTest.Child.class,
}
)
@SessionFactory
@TestForIssue(jiraKey = "HHH-16189")
public class OrderByAndAggregateFunctionTest {
@Test
@RequiresDialect(PostgreSQLDialect.class)
@RequiresDialect(H2Dialect.class)
public void testSelectWithOrderAndGroupBy(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id,c.name, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"order by c.name",
Object[].class
)
.getResultList();
}
);
}
@Test
public void testSelectWithOrderAndGroupBy2(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"order by c.id",
Object[].class
)
.getResultList();
}
);
}
@Test
public void testSelectWithHaving(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery(
"select c.id, sum(p.number) " +
"from Parent p " +
"inner join p.child c " +
"group by c.id " +
"having c.id > 5",
Object[].class
)
.getResultList();
}
);
}
@Entity(name = "Parent")
public static class Parent {
@Id
private String code;
@ManyToOne
private Child child;
@Column(name = "NUMBER_COLUMN")
int number;
}
@Entity(name = "Child")
public static class Child {
@Id
private Long id;
private String name;
}
}