HHH-18830 test demonstrating bug in @OrderColumn

and that it doesn't exist with @OrderBy

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-11-08 10:42:25 +01:00
parent 5adca84c43
commit 38355ccfc8
4 changed files with 178 additions and 0 deletions

View File

@ -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<Page> 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() {
}
}

View File

@ -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<String> queries = ((SQLStatementInspector) scope.getStatementInspector()).getSqlQueries();
assertEquals( 4, queries.size() );
scope.inTransaction( em -> {
assertEquals( em.find( Book.class, "XXX" ).pages.size(), 2 );
} );
}
}

View File

@ -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<Page> 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() {
}
}

View File

@ -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<String> queries = ((SQLStatementInspector) scope.getStatementInspector()).getSqlQueries();
assertEquals( 4, queries.size() );
scope.inTransaction( em -> {
assertEquals( em.find( Book.class, "XXX" ).pages.size(), 2 );
} );
}
}