From 38355ccfc8f3c5dea14cfccd666b67b794b7ad17 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 8 Nov 2024 10:42:25 +0100 Subject: [PATCH] HHH-18830 test demonstrating bug in @OrderColumn and that it doesn't exist with @OrderBy Signed-off-by: Gavin King --- .../orm/test/collection/orderby/Book.java | 53 ++++++++++++++++++ .../orderby/OrderByMappedByTest.java | 35 ++++++++++++ .../orm/test/collection/ordercol/Book.java | 55 +++++++++++++++++++ .../ordercol/OrderColumnMappedByTest.java | 35 ++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/Book.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/OrderByMappedByTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/Book.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/OrderColumnMappedByTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/Book.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/Book.java new file mode 100644 index 0000000000..ce26f0177c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/Book.java @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.collection.orderby; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; +import jakarta.validation.constraints.Size; + +import java.util.ArrayList; +import java.util.List; + +import static jakarta.persistence.CascadeType.PERSIST; + +@Entity +class Book { + @Id + @Size(min=10, max = 13) + String isbn; + + @OneToMany(cascade = PERSIST, + mappedBy = "isbn") + @OrderBy("number") + List pages; + + Book(String isbn) { + this.isbn = isbn; + pages = new ArrayList<>(); + } + + Book() { + } +} + +@Entity +class Page { + @Id String isbn; + @Id @Column(name = "page_number") int number; + String text; + + Page(String isbn, int number, String text) { + this.isbn = isbn; + this.number = number; + this.text = text; + } + + Page() { + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/OrderByMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/OrderByMappedByTest.java new file mode 100644 index 0000000000..590e0d23d8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/orderby/OrderByMappedByTest.java @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.collection.orderby; + +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Jpa(annotatedClasses = {Page.class, Book.class}, + useCollectingStatementInspector = true) +public class OrderByMappedByTest { + @Test void test(EntityManagerFactoryScope scope) { + scope.inTransaction( em -> { + Book book = new Book( "XXX" ); + em.persist( book ); + book.pages.add( new Page("XXX", 1, "Lorem ipsum") ); + } ); + scope.inTransaction( em -> { + Book book = em.find( Book.class, "XXX" ); + book.pages.add( new Page("XXX", 2, "Lorem ipsum") ); + } ); + List queries = ((SQLStatementInspector) scope.getStatementInspector()).getSqlQueries(); + assertEquals( 4, queries.size() ); + scope.inTransaction( em -> { + assertEquals( em.find( Book.class, "XXX" ).pages.size(), 2 ); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/Book.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/Book.java new file mode 100644 index 0000000000..522af2bc1e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/Book.java @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.collection.ordercol; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderColumn; +import jakarta.validation.constraints.Size; +import org.hibernate.annotations.ListIndexBase; + +import java.util.ArrayList; +import java.util.List; + +import static jakarta.persistence.CascadeType.PERSIST; + +@Entity +class Book { + @Id + @Size(min=10, max = 13) + String isbn; + + @OneToMany(cascade = PERSIST, + mappedBy = "isbn") + @OrderColumn(name = "page_number") + @ListIndexBase(1) + List pages; + + Book(String isbn) { + this.isbn = isbn; + pages = new ArrayList<>(); + } + + Book() { + } +} + +@Entity +class Page { + @Id String isbn; + @Id @Column(name = "page_number") int number; + String text; + + Page(String isbn, int number, String text) { + this.isbn = isbn; + this.number = number; + this.text = text; + } + + Page() { + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/OrderColumnMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/OrderColumnMappedByTest.java new file mode 100644 index 0000000000..817d985dc0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordercol/OrderColumnMappedByTest.java @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.collection.ordercol; + +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Jpa(annotatedClasses = {Page.class, Book.class}, + useCollectingStatementInspector = true) +public class OrderColumnMappedByTest { + @Test void test(EntityManagerFactoryScope scope) { + scope.inTransaction( em -> { + Book book = new Book( "XXX" ); + em.persist( book ); + book.pages.add( new Page("XXX", 1, "Lorem ipsum") ); + } ); + scope.inTransaction( em -> { + Book book = em.find( Book.class, "XXX" ); + book.pages.add( new Page("XXX", 2, "Lorem ipsum") ); + } ); + List queries = ((SQLStatementInspector) scope.getStatementInspector()).getSqlQueries(); + assertEquals( 4, queries.size() ); + scope.inTransaction( em -> { + assertEquals( em.find( Book.class, "XXX" ).pages.size(), 2 ); + } ); + } +}