diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/records/ElementCollectionOfRecordsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/records/ElementCollectionOfRecordsTest.java new file mode 100644 index 000000000000..318a6bef3cad --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/records/ElementCollectionOfRecordsTest.java @@ -0,0 +1,88 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.records; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.OrderColumn; +import jakarta.persistence.OrderBy; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Jan Schatteman + */ +@SessionFactory +@DomainModel(annotatedClasses = {ElementCollectionOfRecordsTest.MainEntity.class}) +public class ElementCollectionOfRecordsTest { + + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-18957" ) + public void testInsertOrderOfRecordsInElementCollection(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + MainEntity me = new MainEntity(); + me.setId( 1L ); + me.addRecord( new Record( "c", "a", "b", 2L ) ); + me.addRecord( new Record( "2c", "a2", "bb", 22L ) ); + session.persist( me ); + } + ); + scope.inTransaction( + session -> { + MainEntity me = session.find( MainEntity.class, 1L ); + List records = me.getRecords(); + assertEquals(2, records.size()); + Record r = records.get( 0 ); + assertEquals("a", r.aField); + assertEquals("b", r.bField); + assertEquals("c", r.cField); + assertEquals(2L, r.longField); + r = records.get( 1 ); + assertEquals("a2", r.aField); + assertEquals("bb", r.bField); + assertEquals("2c", r.cField); + assertEquals(22L, r.longField); + } + ); + } + + @Entity(name = "MainEntity") + public static class MainEntity { + @Id + Long id; + + @OrderColumn + @ElementCollection(fetch = FetchType.EAGER) + @OrderBy("longField") + List records = new ArrayList<>(); + + public void setId(Long id) { + this.id = id; + } + + public void addRecord(Record r) { + this.records.add( r ); + } + + public List getRecords() { + return records; + } + } + + @Embeddable + public record Record(String cField, String aField, String bField, Long longField) {} +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEnbeddedIdRecordTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEmbeddedIdRecordTest.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEnbeddedIdRecordTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEmbeddedIdRecordTest.java index 43e9d019a5fc..8e42ef0eb64c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEnbeddedIdRecordTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/records/GenericEmbeddedIdRecordTest.java @@ -28,8 +28,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; @SessionFactory -@DomainModel(annotatedClasses = {GenericEnbeddedIdRecordTest.MainEntity.class, GenericEnbeddedIdRecordTest.ReferencedEntity.class}) -class GenericEnbeddedIdRecordTest { +@DomainModel(annotatedClasses = {GenericEmbeddedIdRecordTest.MainEntity.class, GenericEmbeddedIdRecordTest.ReferencedEntity.class}) +class GenericEmbeddedIdRecordTest { @Test void testOverrideJoinColumn(SessionFactoryScope scope) {