From 4e216a121c21b5aa1fe72e0b09ee45d8e6e3295e Mon Sep 17 00:00:00 2001 From: Fabio Massimo Ercoli Date: Tue, 26 Oct 2021 22:31:13 +0200 Subject: [PATCH] Test embedded > to-one | embedded --- .../OrderByEmbeddableToOneTest.java | 157 ++++++++++++++++++ .../embeddable/OrderByEmbeddableX2Test.java | 148 +++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableToOneTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableX2Test.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableToOneTest.java new file mode 100644 index 0000000000..492df56fe6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableToOneTest.java @@ -0,0 +1,157 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * 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.embeddable; + +import java.util.ArrayList; +import java.util.List; +import jakarta.persistence.Basic; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OrderBy; + +import org.hibernate.Session; +import org.hibernate.query.Query; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OrderByEmbeddableToOneTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Containing.class, Embed.class, Contained.class }; + } + + @Test + public void test() { + inTransaction( session -> { + saveComposition( session, 1 ); + saveComposition( session, 11 ); + saveComposition( session, 21 ); + } ); + + inTransaction( session -> { + Query query = session.createQuery( "select c from containing c order by c.id asc", Containing.class ); + + List resultList = query.getResultList(); + assertThat( resultList ).hasSize( 3 ); + assertThat( resultList.get( 0 ).getEmbeds() ) + .extracting( "contained" ).extracting( "id" ).containsExactly( 3, 2, 1 ); + assertThat( resultList.get( 1 ).getEmbeds() ) + .extracting( "contained" ).extracting( "id" ).containsExactly( 13, 12, 11 ); + assertThat( resultList.get( 2 ).getEmbeds() ) + .extracting( "contained" ).extracting( "id" ).containsExactly( 23, 22, 21 ); + } ); + } + + private void saveComposition(Session session, int id) { + Containing containing = new Containing(); + containing.setId( id ); + + Embed embed1 = new Embed(); + Embed embed2 = new Embed(); + Embed embed3 = new Embed(); + ArrayList embeds = new ArrayList<>(); + embeds.add( embed1 ); + embeds.add( embed2 ); + embeds.add( embed3 ); + containing.setEmbeds( embeds ); + + Contained contained1 = new Contained(); + contained1.setId( id ); + contained1.setData( "data" + id++ ); + Contained contained2 = new Contained(); + contained2.setId( id ); + contained2.setData( "data" + id++ ); + Contained contained3 = new Contained(); + contained3.setId( id ); + contained3.setData( "data" + id++ ); + + embed2.setContained( contained2 ); + embed3.setContained( contained3 ); + embed1.setContained( contained1 ); + + session.persist( containing ); + session.persist( contained1 ); + session.persist( contained2 ); + session.persist( contained3 ); + } + + @Entity(name = "containing") + public static class Containing { + + @Id + private Integer id; + + @ElementCollection + @OrderBy("contained.id desc") + private List embeds = new ArrayList<>(); + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getEmbeds() { + return embeds; + } + + public void setEmbeds(List embeds) { + this.embeds = embeds; + } + } + + @Embeddable + public static class Embed { + + @ManyToOne + private Contained contained; + + public Contained getContained() { + return contained; + } + + public void setContained(Contained contained) { + this.contained = contained; + } + } + + @Entity(name = "contained") + public static class Contained { + + @Id + private Integer id; + + @Basic + private String data; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableX2Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableX2Test.java new file mode 100644 index 0000000000..8a539f6113 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/OrderByEmbeddableX2Test.java @@ -0,0 +1,148 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * 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.embeddable; + +import java.util.ArrayList; +import java.util.List; +import jakarta.persistence.Basic; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OrderBy; + +import org.hibernate.Session; +import org.hibernate.query.Query; + +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OrderByEmbeddableX2Test extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Containing.class, Embed.class, Contained.class }; + } + + @Test + public void test() { + inTransaction( session -> { + saveComposition( session, 1 ); + saveComposition( session, 11 ); + saveComposition( session, 21 ); + } ); + + inTransaction( session -> { + Query query = session.createQuery( "select c from containing c order by c.id asc", Containing.class ); + + List resultList = query.getResultList(); + assertThat( resultList ).hasSize( 3 ); + assertThat( resultList.get( 0 ).getEmbeds() ) + .extracting( "contained" ).extracting( "data" ).containsExactly( "data3", "data2", "data1" ); + assertThat( resultList.get( 1 ).getEmbeds() ) + .extracting( "contained" ).extracting( "data" ).containsExactly( "data13", "data12", "data11" ); + assertThat( resultList.get( 2 ).getEmbeds() ) + .extracting( "contained" ).extracting( "data" ).containsExactly( "data23", "data22", "data21" ); + } ); + } + + private void saveComposition(Session session, int id) { + Containing containing = new Containing(); + containing.setId( id ); + + Embed embed1 = new Embed(); + Embed embed2 = new Embed(); + Embed embed3 = new Embed(); + ArrayList embeds = new ArrayList<>(); + embeds.add( embed1 ); + embeds.add( embed2 ); + embeds.add( embed3 ); + containing.setEmbeds( embeds ); + + Contained contained1 = new Contained(); + contained1.setData( "data" + id++ ); + Contained contained2 = new Contained(); + contained2.setData( "data" + id++ ); + Contained contained3 = new Contained(); + contained3.setData( "data" + id++ ); + + embed1.setContained( contained1 ); + embed2.setContained( contained2 ); + embed3.setContained( contained3 ); + + session.persist( containing ); + } + + @Entity(name = "containing") + public static class Containing { + + @Id + private Integer id; + + @ElementCollection + @OrderBy("contained.data desc") + private List embeds = new ArrayList<>(); + + public Containing() { + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getEmbeds() { + return embeds; + } + + public void setEmbeds(List embeds) { + this.embeds = embeds; + } + } + + @Embeddable + public static class Embed { + + @Embedded + private Contained contained; + + public Embed() { + } + + public Contained getContained() { + return contained; + } + + public void setContained(Contained contained) { + this.contained = contained; + } + } + + @Embeddable + public static class Contained { + + @Basic + private String data; + + public Contained() { + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + } +}