diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Customer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Customer.java new file mode 100644 index 0000000000..7d13d2289f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Customer.java @@ -0,0 +1,42 @@ +/* + * 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.mapping.cid.query; + +import jakarta.persistence.Basic; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity(name = "Customer") +@Table(name = "customers") +public class Customer { + @Id + public Integer id; + @Basic + public String name; + + protected Customer() { + // for Hibernate use + } + + public Customer(Integer id, String name) { + this.id = id; + this.name = name; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/EmbeddableInitializerTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/EmbeddableInitializerTests.java new file mode 100644 index 0000000000..045345d0e0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/EmbeddableInitializerTests.java @@ -0,0 +1,83 @@ +/* + * 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.mapping.cid.query; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Steve Ebersole + */ +@DomainModel( annotatedClasses = { Customer.class, Order.class } ) +@SessionFactory +public class EmbeddableInitializerTests { + + @Test + public void testHqlVirtualIdReferences(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "from Order o where o.orderNumber = 123" ).list(); + session.createQuery( "from Order o where o.customer = 1" ).list(); + session.createQuery( "from Order o where o.customer.id = 1" ).list(); + } ); + } + + @Test + public void testHqlIdClassReferences(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "from Order o where o.id.orderNumber = 123" ).list(); + } ); + } + + @Test + @FailureExpected( + reason = "Mismatched expectations - some parts of the translation expect this to be the key-many-to-one" + + " while other parts expect the basic IdClass reference" + ) + public void testHqlIdClassReferencesBug(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "from Order o where o.id.customer = 1" ).list(); + } ); + } + + @Test + @FailureExpected( reason = "This is an invalid query" ) + public void testHqlInvalidIdClassReferences(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "from Order o where o.id.customer.id = 1" ).list(); + } ); + } + + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + final Customer acme = new Customer( 1, "acme" ); + final Customer spacely = new Customer( 2, "spacely" ); + session.persist( acme ); + session.persist( spacely ); + + final Order acmeOrder1 = new Order( acme, 1, 123F ); + final Order acmeOrder2 = new Order( acme, 2, 123F ); + session.persist( acmeOrder1 ); + session.persist( acmeOrder2 ); + } ); + } + + @AfterEach + public void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + session.createQuery( "delete Order" ).executeUpdate(); + session.createQuery( "delete Customer" ).executeUpdate(); + } ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Order.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Order.java new file mode 100644 index 0000000000..e8b2926bcc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/Order.java @@ -0,0 +1,61 @@ +/* + * 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.mapping.cid.query; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; + +@Entity(name = "Order") +@Table(name = "orders") +@IdClass( OrderId.class ) +public class Order { + @Id + @ManyToOne + public Customer customer; + + @Id + public Integer orderNumber; + + public Float amount; + + protected Order() { + // for Hibernate use + } + + public Order(Customer customer, Integer orderNumber, Float amount) { + this.customer = customer; + this.orderNumber = orderNumber; + this.amount = amount; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public Integer getOrderNumber() { + return orderNumber; + } + + public void setOrderNumber(Integer orderNumber) { + this.orderNumber = orderNumber; + } + + public Float getAmount() { + return amount; + } + + public void setAmount(Float amount) { + this.amount = amount; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/OrderId.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/OrderId.java new file mode 100644 index 0000000000..0d48fe17c6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/OrderId.java @@ -0,0 +1,20 @@ +/* + * 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.mapping.cid.query; + +public class OrderId { + Integer customer; + Integer orderNumber; + + public OrderId() { + } + + public OrderId(Integer customer, Integer orderNumber) { + this.customer = customer; + this.orderNumber = orderNumber; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/package-info.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/package-info.java new file mode 100644 index 0000000000..a2fd48fd7d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/cid/query/package-info.java @@ -0,0 +1,14 @@ +/* + * 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 + */ + +/** + * Simplified form of {@link org.hibernate.orm.test.mapping.cid.idclass} + * for the purpose of tracking down seemingly duplicated calls to the + * {@link org.hibernate.sql.results.graph.embeddable.EmbeddableInitializer} + * impls + */ +package org.hibernate.orm.test.mapping.cid.query; \ No newline at end of file