From ff1fc1045416a6d812f244377d10f7879b38284b Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 21 Aug 2020 07:58:24 +0100 Subject: [PATCH] Fix issue with collection @OrderBy --- .../mapping/internal/AbstractDomainPath.java | 186 ++++++++++++++++++ .../mapping/ordering/OrderByFragmentImpl.java | 1 + .../ordering/ast/CollectionPartPath.java | 3 +- .../mapping/ordering/ast/ColumnReference.java | 1 + .../mapping/ordering/ast/DomainPath.java | 70 ------- .../ordering/ast/DomainPathContinuation.java | 3 +- .../ordering/ast/FunctionExpression.java | 1 + .../ordering/ast/OrderingExpression.java | 2 +- .../ordering/ast/OrderingSpecification.java | 16 +- .../ordering/ast/ParseTreeVisitor.java | 2 +- .../ordering/ast/PluralAttributePath.java | 9 +- .../collectionelement/ordered/Person.java | 12 +- .../ordered/joinedInheritence/Animal.java | 2 +- .../ordered/joinedInheritence/Lion.java | 2 +- .../OrderCollectionOfJoinedHierarchyTest.java | 74 +++++++ .../ordered/joinedInheritence/Tiger.java | 59 ++++++ .../ordered/joinedInheritence/Zoo.java | 11 +- .../OrderCollectionOfJoinedHierarchyTest.java | 74 ------- .../ordered/joinedInheritence/Tiger.java | 25 --- 19 files changed, 360 insertions(+), 193 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDomainPath.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/collection/ordered/joinedInheritence/Animal.java (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/collection/ordered/joinedInheritence/Lion.java (83%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Tiger.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/collection/ordered/joinedInheritence/Zoo.java (85%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Tiger.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDomainPath.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDomainPath.java new file mode 100644 index 0000000000..e3992afa80 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDomainPath.java @@ -0,0 +1,186 @@ +/* + * 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.metamodel.mapping.internal; + +import org.hibernate.NotYetImplementedFor6Exception; +import org.hibernate.SortOrder; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.mapping.BasicValuedModelPart; +import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; +import org.hibernate.metamodel.mapping.EntityValuedModelPart; +import org.hibernate.metamodel.mapping.ModelPart; +import org.hibernate.metamodel.mapping.ordering.ast.DomainPath; +import org.hibernate.sql.ast.spi.SqlAstCreationContext; +import org.hibernate.sql.ast.spi.SqlAstCreationState; +import org.hibernate.sql.ast.spi.SqlExpressionResolver; +import org.hibernate.sql.ast.tree.expression.ColumnReference; +import org.hibernate.sql.ast.tree.from.TableGroup; +import org.hibernate.sql.ast.tree.from.TableReference; +import org.hibernate.sql.ast.tree.select.QuerySpec; +import org.hibernate.sql.ast.tree.select.SortSpecification; + +/** + * @author Andrea Boriero + */ +public abstract class AbstractDomainPath implements DomainPath { + public static final String ELEMENT_TOKEN = "$element$"; + + @Override + public void apply( + QuerySpec ast, + TableGroup tableGroup, + String collation, + String modelPartName, + SortOrder sortOrder, + SqlAstCreationState creationState) { + final SqlAstCreationContext creationContext = creationState.getCreationContext(); + apply( + getReferenceModelPart(), + ast, + tableGroup, + collation, + modelPartName, + sortOrder, + creationState, + creationContext.getSessionFactory(), + creationState.getSqlExpressionResolver() + ); + } + + public void apply( + ModelPart referenceModelPart, + QuerySpec ast, + TableGroup tableGroup, + String collation, + String modelPartName, + SortOrder sortOrder, + SqlAstCreationState creationState, + SessionFactoryImplementor sessionFactory, + SqlExpressionResolver sqlExprResolver) { + if ( referenceModelPart instanceof BasicValuedModelPart ) { + assSortSpecification( + (BasicValuedModelPart) referenceModelPart, + ast, + tableGroup, + collation, + sortOrder, + creationState + ); + } + else if ( referenceModelPart instanceof EntityValuedModelPart ) { + final ModelPart subPart; + if ( ELEMENT_TOKEN.equals( modelPartName ) ) { + subPart = ( (EntityValuedModelPart) referenceModelPart ).getEntityMappingType().getIdentifierMapping(); + } + else { + subPart = ( (EntityValuedModelPart) referenceModelPart ).findSubPart( modelPartName ); + } + apply( + subPart, + ast, + tableGroup, + collation, + modelPartName, + sortOrder, + creationState, + sessionFactory, + sqlExprResolver + ); + } + else if ( referenceModelPart instanceof EmbeddableValuedModelPart ) { + assSortSpecification( + (EmbeddableValuedModelPart) referenceModelPart, + ast, + tableGroup, + collation, + modelPartName, + sortOrder, + creationState, + sessionFactory, + sqlExprResolver + ); + } + else { + throw new NotYetImplementedFor6Exception( "Ordering for " + getReferenceModelPart() + "not yet supported" ); + } + } + + private void assSortSpecification( + EmbeddableValuedModelPart embeddableValuedModelPart, + QuerySpec ast, + TableGroup tableGroup, + String collation, + String modelPartName, + SortOrder sortOrder, + SqlAstCreationState creationState, + SessionFactoryImplementor sessionFactory, + SqlExpressionResolver sqlExprResolver) { + if ( embeddableValuedModelPart.getFetchableName() + .equals( modelPartName ) || ELEMENT_TOKEN.equals( modelPartName ) ) { + embeddableValuedModelPart.visitColumns( + (tableExpression, columnExpression, isColumnExpressionFormula, jdbcMapping) -> { + final TableReference tableReference = tableGroup.resolveTableReference( tableExpression ); + ast.addSortSpecification( + new SortSpecification( + sqlExprResolver.resolveSqlExpression( + SqlExpressionResolver.createColumnReferenceKey( + tableExpression, + columnExpression + ), + sqlAstProcessingState -> new ColumnReference( + tableReference, + columnExpression, + isColumnExpressionFormula, + jdbcMapping, + sessionFactory + ) + ), + collation, + sortOrder + ) + ); + } + ); + } + else { + ModelPart subPart = embeddableValuedModelPart.findSubPart( modelPartName, null ); + assert subPart instanceof BasicValuedModelPart; + assSortSpecification( + (BasicValuedModelPart) subPart, + ast, + tableGroup, + collation, + sortOrder, + creationState + ); + } + } + + private void assSortSpecification( + BasicValuedModelPart basicValuedPart, + QuerySpec ast, + TableGroup tableGroup, + String collation, + SortOrder sortOrder, + SqlAstCreationState creationState) { + final TableReference tableReference = tableGroup.resolveTableReference( basicValuedPart.getContainingTableExpression() ); + + ast.addSortSpecification( + new SortSpecification( + new ColumnReference( + tableReference, + basicValuedPart.getMappedColumnExpression(), + basicValuedPart.isMappedColumnExpressionFormula(), + basicValuedPart.getJdbcMapping(), + creationState.getCreationContext().getSessionFactory() + ), + collation, + sortOrder + ) + ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/OrderByFragmentImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/OrderByFragmentImpl.java index 17a801f1f9..bc303e99c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/OrderByFragmentImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/OrderByFragmentImpl.java @@ -36,6 +36,7 @@ public class OrderByFragmentImpl implements OrderByFragment { ast, tableGroup, orderingSpec.getCollation(), + orderingSpec.getOrderByValue(), orderingSpec.getSortOrder(), creationState ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/CollectionPartPath.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/CollectionPartPath.java index 62947d66f6..349db44d7b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/CollectionPartPath.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/CollectionPartPath.java @@ -9,6 +9,7 @@ package org.hibernate.metamodel.mapping.ordering.ast; import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.PluralAttributeMapping; +import org.hibernate.metamodel.mapping.internal.AbstractDomainPath; import org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart; import org.hibernate.metamodel.mapping.ordering.TranslationContext; import org.hibernate.query.NavigablePath; @@ -18,7 +19,7 @@ import org.hibernate.query.NavigablePath; * * @author Steve Ebersole */ -public class CollectionPartPath implements DomainPath { +public class CollectionPartPath extends AbstractDomainPath { private final NavigablePath navigablePath; private final PluralAttributePath lhs; private final CollectionPart referencedPart; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ColumnReference.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ColumnReference.java index 5b0d336653..d19765f47a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ColumnReference.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ColumnReference.java @@ -62,6 +62,7 @@ public class ColumnReference implements OrderingExpression, SequencePart { QuerySpec ast, TableGroup tableGroup, String collation, + String modelPartName, SortOrder sortOrder, SqlAstCreationState creationState) { TableReference tableReference; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPath.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPath.java index ff91a98a5a..b06731133c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPath.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPath.java @@ -6,20 +6,9 @@ */ package org.hibernate.metamodel.mapping.ordering.ast; -import org.hibernate.SortOrder; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.metamodel.mapping.BasicValuedModelPart; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.query.NavigablePath; -import org.hibernate.sql.ast.spi.SqlAstCreationContext; -import org.hibernate.sql.ast.spi.SqlAstCreationState; -import org.hibernate.sql.ast.spi.SqlExpressionResolver; -import org.hibernate.sql.ast.tree.expression.ColumnReference; -import org.hibernate.sql.ast.tree.from.TableGroup; -import org.hibernate.sql.ast.tree.from.TableReference; -import org.hibernate.sql.ast.tree.select.QuerySpec; -import org.hibernate.sql.ast.tree.select.SortSpecification; /** * Represents a domain-path (model part path) used in an order-by fragment @@ -36,63 +25,4 @@ public interface DomainPath extends OrderingExpression, SequencePart { default PluralAttributeMapping getPluralAttribute() { return getLhs().getPluralAttribute(); } - - @Override - default void apply( - QuerySpec ast, - TableGroup tableGroup, - String collation, - SortOrder sortOrder, - SqlAstCreationState creationState) { - final SqlAstCreationContext creationContext = creationState.getCreationContext(); - final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory(); - final SqlExpressionResolver sqlExprResolver = creationState.getSqlExpressionResolver(); - - if ( getReferenceModelPart() instanceof BasicValuedModelPart ) { - final BasicValuedModelPart basicValuedPart = (BasicValuedModelPart) getReferenceModelPart(); - - final TableReference tableReference = tableGroup.resolveTableReference( basicValuedPart.getContainingTableExpression() ); - - ast.addSortSpecification( - new SortSpecification( - new ColumnReference( - tableReference, - basicValuedPart.getMappedColumnExpression(), - basicValuedPart.isMappedColumnExpressionFormula(), - basicValuedPart.getJdbcMapping(), - creationState.getCreationContext().getSessionFactory() - ), - collation, - sortOrder - ) - ); - } - else { - getReferenceModelPart().visitColumns( - (tableExpression, columnExpression, isColumnExpressionFormula, jdbcMapping) -> { - final TableReference tableReference = tableGroup.resolveTableReference( tableExpression ); - ast.addSortSpecification( - new SortSpecification( - sqlExprResolver.resolveSqlExpression( - SqlExpressionResolver.createColumnReferenceKey( - tableExpression, - columnExpression - ), - sqlAstProcessingState -> new ColumnReference( - tableReference, - columnExpression, - isColumnExpressionFormula, - jdbcMapping, - sessionFactory - ) - ), - collation, - sortOrder - ) - ); - } - ); - } - } - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPathContinuation.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPathContinuation.java index c5a7f78707..47dea20ce0 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPathContinuation.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/DomainPathContinuation.java @@ -9,6 +9,7 @@ package org.hibernate.metamodel.mapping.ordering.ast; import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.ModelPart; +import org.hibernate.metamodel.mapping.internal.AbstractDomainPath; import org.hibernate.metamodel.mapping.ordering.TranslationContext; import org.hibernate.query.NavigablePath; @@ -17,7 +18,7 @@ import org.hibernate.query.NavigablePath; * * @author Steve Ebersole */ -public class DomainPathContinuation implements DomainPath { +public class DomainPathContinuation extends AbstractDomainPath { private final NavigablePath navigablePath; private final DomainPath lhs; private final ModelPart referencedModelPart; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/FunctionExpression.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/FunctionExpression.java index 13ec066d5a..71875a4af6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/FunctionExpression.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/FunctionExpression.java @@ -49,6 +49,7 @@ public class FunctionExpression implements OrderingExpression { QuerySpec ast, TableGroup tableGroup, String collation, + String modelPartName, SortOrder sortOrder, SqlAstCreationState creationState) { throw new NotYetImplementedFor6Exception( getClass() ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingExpression.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingExpression.java index d8de335dae..59cb6e2dfe 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingExpression.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingExpression.java @@ -20,5 +20,5 @@ public interface OrderingExpression extends Node { /** * Apply the SQL AST sort-specifications associated with this ordering-expression */ - void apply(QuerySpec ast, TableGroup tableGroup, String collation, SortOrder sortOrder, SqlAstCreationState creationState); + void apply(QuerySpec ast, TableGroup tableGroup, String collation, String modelPartName, SortOrder sortOrder, SqlAstCreationState creationState); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingSpecification.java index f9c25875bd..9820453f6f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingSpecification.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/OrderingSpecification.java @@ -20,13 +20,19 @@ public class OrderingSpecification implements Node { private String collation; private SortOrder sortOrder; private NullPrecedence nullPrecedence = NullPrecedence.NONE; + private String orderByValue; - public OrderingSpecification(OrderingExpression orderingExpression) { - this.orderingExpression = orderingExpression; + public String getOrderByValue() { + return orderByValue; } - public OrderingSpecification(OrderingExpression orderingExpression, SortOrder sortOrder) { + public OrderingSpecification(OrderingExpression orderingExpression, String orderByValue) { this.orderingExpression = orderingExpression; + this.orderByValue = orderByValue; + } + + public OrderingSpecification(OrderingExpression orderingExpression, String orderByValue,SortOrder sortOrder) { + this(orderingExpression, orderByValue); this.sortOrder = sortOrder; } @@ -57,4 +63,8 @@ public class OrderingSpecification implements Node { public void setNullPrecedence(NullPrecedence nullPrecedence) { this.nullPrecedence = nullPrecedence; } + + public void setOrderByValue(String orderByValue) { + this.orderByValue = orderByValue; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ParseTreeVisitor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ParseTreeVisitor.java index bc9e993886..d1694393f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ParseTreeVisitor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/ParseTreeVisitor.java @@ -70,7 +70,7 @@ public class ParseTreeVisitor extends OrderingParserBaseVisitor { } } - final OrderingSpecification result = new OrderingSpecification( orderingExpression ); + final OrderingSpecification result = new OrderingSpecification( orderingExpression, parsedSpec.expression().getText()); if ( parsedSpec.collationSpecification() != null ) { result.setCollation( parsedSpec.collationSpecification().identifier().getText() ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/PluralAttributePath.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/PluralAttributePath.java index 6917d83e0e..108b3011a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/PluralAttributePath.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/ordering/ast/PluralAttributePath.java @@ -10,17 +10,17 @@ import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.PluralAttributeMapping; +import org.hibernate.metamodel.mapping.internal.AbstractDomainPath; import org.hibernate.metamodel.mapping.ordering.TranslationContext; import org.hibernate.query.NavigablePath; /** * Represents the collection as a DomainPath * - * @see RootSequencePart - * * @author Steve Ebersole + * @see RootSequencePart */ -public class PluralAttributePath implements DomainPath { +public class PluralAttributePath extends AbstractDomainPath { private final NavigablePath navigablePath; private final PluralAttributeMapping pluralAttributeMapping; @@ -67,6 +67,9 @@ public class PluralAttributePath implements DomainPath { pluralAttributeMapping.getElementDescriptor() ); } + else { + return new DomainPathContinuation( navigablePath.append( name ), this, subPart ); + } } // the above checks for explicit element or index descriptor references diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/ordered/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/ordered/Person.java index 0ecb44699c..d91f868ee2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/ordered/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/ordered/Person.java @@ -27,13 +27,13 @@ public class Person { private Long id; private String name; - private Set nickNamesAscendingNaturalSort = new HashSet(); - private Set nickNamesDescendingNaturalSort = new HashSet(); + private Set nickNamesAscendingNaturalSort = new HashSet<>(); + private Set nickNamesDescendingNaturalSort = new HashSet<>(); - private Set
addressesAscendingNaturalSort = new HashSet
(); - private Set
addressesDescendingNaturalSort = new HashSet
(); - private Set
addressesCityAscendingSort = new HashSet
(); - private Set
addressesCityDescendingSort = new HashSet
(); + private Set
addressesAscendingNaturalSort = new HashSet<>(); + private Set
addressesDescendingNaturalSort = new HashSet<>(); + private Set
addressesCityAscendingSort = new HashSet<>(); + private Set
addressesCityDescendingSort = new HashSet<>(); @Id diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Animal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Animal.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Animal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Animal.java index 8c908d0cd5..598968f0fc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Animal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Animal.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.collection.ordered.joinedInheritence; +package org.hibernate.orm.test.collection.ordered.joinedInheritence; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Lion.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Lion.java similarity index 83% rename from hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Lion.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Lion.java index a207d8d60a..08e5bd35f8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Lion.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Lion.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.collection.ordered.joinedInheritence; +package org.hibernate.orm.test.collection.ordered.joinedInheritence; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java new file mode 100644 index 0000000000..a9a5530712 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java @@ -0,0 +1,74 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.collection.ordered.joinedInheritence; + +import org.hibernate.internal.util.collections.CollectionHelper; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + * @author Brett Meyer + */ +@DomainModel( + annotatedClasses = { + Animal.class, Lion.class, Tiger.class, Zoo.class + } +) +@SessionFactory +public class OrderCollectionOfJoinedHierarchyTest { + + @Test + public void testQuerySyntaxCheck(SessionFactoryScope scope) { + scope.inTransaction( + session -> session.get( Zoo.class, 1L ) + ); + } + + @Test + public void testOrdering(SessionFactoryScope scope) { + Zoo zoo = new Zoo(); + Lion lion1 = new Lion(); + lion1.setWeight( 2 ); + Lion lion2 = new Lion(); + lion2.setWeight( 1 ); + zoo.getLions().add( lion1 ); + zoo.getLions().add( lion2 ); + zoo.getAnimalsById().add( lion1 ); + zoo.getAnimalsById().add( lion2 ); + + Zoo zoo1 = scope.fromTransaction( + session -> { + session.persist( lion1 ); + session.persist( lion2 ); + session.persist( zoo ); + session.getTransaction().commit(); + session.clear(); + + session.beginTransaction(); + Zoo z = session.get( Zoo.class, zoo.getId() ); + z.getLions().size(); + z.getTigers().size(); + z.getAnimalsById().size(); + return z; + } + ); + + assertNotNull( zoo1 ); + assertTrue( CollectionHelper.isNotEmpty( zoo1.getLions() ) && zoo1.getLions().size() == 2 ); + assertTrue( CollectionHelper.isNotEmpty( zoo1.getAnimalsById() ) && zoo1.getAnimalsById().size() == 2 ); + assertEquals( zoo1.getLions().iterator().next().getId(), lion2.getId() ); + assertEquals( zoo1.getAnimalsById().iterator().next().getId(), lion1.getId() ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Tiger.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Tiger.java new file mode 100644 index 0000000000..d3b0648b01 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Tiger.java @@ -0,0 +1,59 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.collection.ordered.joinedInheritence; + +import javax.persistence.Embeddable; +import javax.persistence.Entity; + +/** + * @author Steve Ebersole + */ +@Entity +public class Tiger extends Animal { + private int numberOfStripes; + + private Details details; + + public int getNumberOfStripes() { + return numberOfStripes; + } + + public void setNumberOfStripes(int numberOfStripes) { + this.numberOfStripes = numberOfStripes; + } + + public Details getDetails() { + return details; + } + + public void setDetails(Details details) { + this.details = details; + } + + @Embeddable + public static class Details { + private String name; + private String sex; + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Zoo.java similarity index 85% rename from hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Zoo.java index 89159da834..fc252d9eb9 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Zoo.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/collection/ordered/joinedInheritence/Zoo.java @@ -4,10 +4,9 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.collection.ordered.joinedInheritence; +package org.hibernate.orm.test.collection.ordered.joinedInheritence; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; @@ -26,9 +25,9 @@ public class Zoo { private Long id; private String name; private String city; - private Set tigers = new HashSet(); - private Set lions = new HashSet(); - private Set animals = new HashSet(); + private Set tigers = new HashSet<>(); + private Set lions = new HashSet<>(); + private Set animals = new HashSet<>(); @Id @GeneratedValue( generator = "increment" ) @@ -59,7 +58,7 @@ public class Zoo { @OneToMany @JoinColumn - @javax.persistence.OrderBy( "weight" ) + @javax.persistence.OrderBy( "details" ) public Set getTigers() { return tigers; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java deleted file mode 100644 index 42c3f83525..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/OrderCollectionOfJoinedHierarchyTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.collection.ordered.joinedInheritence; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.hibernate.Session; - -import org.junit.Test; - -import org.hibernate.internal.util.collections.CollectionHelper; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -/** - * @author Steve Ebersole - * @author Brett Meyer - */ -public class OrderCollectionOfJoinedHierarchyTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Animal.class, Lion.class, Tiger.class, Zoo.class }; - } - - @Test - public void testQuerySyntaxCheck() { - Session session = openSession(); - session.beginTransaction(); - session.get( Zoo.class, 1L ); - session.getTransaction().commit(); - session.close(); - } - - @Test - public void testOrdering() { - Zoo zoo = new Zoo(); - Lion lion1 = new Lion(); - lion1.setWeight( 2 ); - Lion lion2 = new Lion(); - lion2.setWeight( 1 ); - zoo.getLions().add( lion1 ); - zoo.getLions().add( lion2 ); - zoo.getAnimalsById().add( lion1 ); - zoo.getAnimalsById().add( lion2 ); - - Session session = openSession(); - session.beginTransaction(); - session.persist( lion1 ); - session.persist( lion2 ); - session.persist( zoo ); - session.getTransaction().commit(); - session.clear(); - - session.beginTransaction(); - zoo = (Zoo) session.get( Zoo.class, zoo.getId() ); - zoo.getLions().size(); - zoo.getLions().size(); - zoo.getAnimalsById().size(); - session.getTransaction().commit(); - session.close(); - - assertNotNull( zoo ); - assertTrue( CollectionHelper.isNotEmpty( zoo.getLions() ) && zoo.getLions().size() == 2 ); - assertTrue( CollectionHelper.isNotEmpty( zoo.getAnimalsById() ) && zoo.getAnimalsById().size() == 2 ); - assertEquals( zoo.getLions().iterator().next().getId(), lion2.getId() ); - assertEquals( zoo.getAnimalsById().iterator().next().getId(), lion1.getId() ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Tiger.java b/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Tiger.java deleted file mode 100644 index c2d2e25b68..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/ordered/joinedInheritence/Tiger.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.test.collection.ordered.joinedInheritence; - -import javax.persistence.Entity; - -/** - * @author Steve Ebersole - */ -@Entity -public class Tiger extends Animal { - private int numberOfStripes; - - public int getNumberOfStripes() { - return numberOfStripes; - } - - public void setNumberOfStripes(int numberOfStripes) { - this.numberOfStripes = numberOfStripes; - } -}