From 013a776e8a90e3b77d9e9b8690e201214c6605df Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 5 Jun 2023 09:29:58 +0200 Subject: [PATCH] HHH-16641 Add test and fix order column on generic non-map associations --- .../model/internal/ClassPropertyHolder.java | 4 +- .../GenericAssociationOrderColumnTest.java | 126 ++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/GenericAssociationOrderColumnTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java index 64f02e8c13..84f791146d 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java @@ -341,7 +341,9 @@ public class ClassPropertyHolder extends AbstractPropertyHolder { setTypeName( element, inferredData.getProperty().getElementClass().getName() ); if ( initializedCollection instanceof IndexedCollection ) { final Value index = ( (IndexedCollection) initializedCollection ).getIndex().copy(); - setTypeName( index, inferredData.getProperty().getMapKey().getName() ); + if ( inferredData.getProperty().getMapKey() != null ) { + setTypeName( index, inferredData.getProperty().getMapKey().getName() ); + } ( (IndexedCollection) collection ).setIndex( index ); } collection.setElement( element ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/GenericAssociationOrderColumnTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/GenericAssociationOrderColumnTest.java new file mode 100644 index 0000000000..1234aa4e2a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/generics/GenericAssociationOrderColumnTest.java @@ -0,0 +1,126 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.annotations.generics; + +import java.util.ArrayList; +import java.util.List; + +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderColumn; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel( annotatedClasses = { + GenericAssociationOrderColumnTest.AbstractParent.class, + GenericAssociationOrderColumnTest.ParentEntity.class, + GenericAssociationOrderColumnTest.ChildEntity.class, +} ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16641" ) +public class GenericAssociationOrderColumnTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final ParentEntity p1 = new ParentEntity( 1L ); + final ChildEntity c1 = new ChildEntity( 2L, p1 ); + p1.getChildren().add( c1 ); + final ChildEntity c2 = new ChildEntity( 3L, p1 ); + p1.getChildren().add( c2 ); + final ChildEntity c3 = new ChildEntity( 4L, p1 ); + p1.getChildren().add( c3 ); + session.persist( p1 ); + } ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createMutationQuery( "delete from ChildEntity" ).executeUpdate(); + session.createMutationQuery( "delete from ParentEntity" ).executeUpdate(); + } ); + } + + @Test + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final ParentEntity parent = session.find( ParentEntity.class, 1L ); + assertThat( parent.getChildren().stream().map( ChildEntity::getId ) ).containsExactly( 2L, 3L, 4L ); + } ); + } + + @MappedSuperclass + public static abstract class AbstractParent { + @Id + public Long id; + + @OneToMany( mappedBy = "parent", cascade = CascadeType.ALL ) + @OrderColumn + public List children = new ArrayList<>(); + + public AbstractParent() { + } + + public AbstractParent(Long id) { + this.id = id; + } + + public List getChildren() { + return children; + } + } + + @Entity( name = "ChildEntity" ) + public static class ChildEntity { + @Id + public Long id; + + @ManyToOne + public ParentEntity parent; + + public ChildEntity() { + } + + public ChildEntity(Long id, ParentEntity parent) { + this.id = id; + this.parent = parent; + } + + public Long getId() { + return id; + } + + public ParentEntity getParent() { + return parent; + } + } + + @Entity( name = "ParentEntity" ) + public static class ParentEntity extends AbstractParent { + public ParentEntity() { + } + + public ParentEntity(Long id) { + super( id ); + } + } +}