From cd9bc2bb226dfaa64648955afa895730cad46c49 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 29 Mar 2024 17:54:55 +0100 Subject: [PATCH] HHH-17881 Add test for issue --- .../QueryWithProxyAsParametersTest.java | 125 ++++++++++++++++++ .../QueryWithProxyAsParametersTest.java | 124 +++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/naturalid/QueryWithProxyAsParametersTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java new file mode 100644 index 0000000000..938a7e72b0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/naturalid/QueryWithProxyAsParametersTest.java @@ -0,0 +1,125 @@ +package org.hibernate.orm.test.bytecode.enhancement.naturalid; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.NaturalId; + +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@RunWith(BytecodeEnhancerRunner.class) +@JiraKey( "HHH-17881" ) +public class QueryWithProxyAsParametersTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Parent.class, + Child.class + }; + } + + @Before + public void setUp(){ + inTransaction( + session -> { + Child child = new Child(1l, "abc", "Andrea"); + Parent parent = new Parent(2l, "Lionello", child); + session.persist( child ); + session.persist( parent ); + } + ); + } + + @Test + public void testQuery(){ + inTransaction( + session -> { + Child child = session.getReference( Child.class, 1l ); + + Parent parent = session.createQuery( "select p from Parent p where p.child = :child", Parent.class ) + .setParameter( "child", child ).uniqueResult(); + + assertThat(parent).isNotNull(); + } + ); + } + + @Entity(name = "Parent") + public static class Parent { + + @Id + private Long id; + + private String name; + + @OneToOne() + @JoinColumn(name = "child_code", referencedColumnName = "child_code") + private Child child; + + public Parent() { + } + + public Parent(Long id, String name, Child child) { + this.id = id; + this.name = name; + this.child = child; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public Child getChild() { + return child; + } + } + + @Entity(name = "Child") + public static class Child { + @Id + private Long id; + + @NaturalId + @Column(name = "child_code") + private String code; + + private String name; + + public Child() { + } + + public Child(Long id, String code, String name) { + this.id = id; + this.code = code; + this.name = name; + } + + public Long getId() { + return id; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/naturalid/QueryWithProxyAsParametersTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/naturalid/QueryWithProxyAsParametersTest.java new file mode 100644 index 0000000000..99d025f6f4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/naturalid/QueryWithProxyAsParametersTest.java @@ -0,0 +1,124 @@ +package org.hibernate.orm.test.query.naturalid; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.NaturalId; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DomainModel( + annotatedClasses = { + QueryWithProxyAsParametersTest.Parent.class, + QueryWithProxyAsParametersTest.Child.class + } +) +@SessionFactory +@JiraKey( "HHH-17881" ) +public class QueryWithProxyAsParametersTest { + + @BeforeAll + public void setUp(SessionFactoryScope scope){ + scope.inTransaction( + session -> { + Child child = new Child(1l, "abc", "Andrea"); + Parent parent = new Parent(2l, "Lionello", child); + session.persist( child ); + session.persist( parent ); + } + ); + } + + @Test + public void testQuery(SessionFactoryScope scope){ + scope.inTransaction( + session -> { + Child child = session.getReference( Child.class, 1l ); + + Parent parent = session.createQuery( "select p from Parent p where p.child = :child", Parent.class ) + .setParameter( "child", child ).uniqueResult(); + + assertThat(parent).isNotNull(); + } + ); + } + + @Entity(name = "Parent") + public static class Parent { + + @Id + private Long id; + + private String name; + + @OneToOne() + @JoinColumn(name = "child_code", referencedColumnName = "child_code") + private Child child; + + public Parent() { + } + + public Parent(Long id, String name, Child child) { + this.id = id; + this.name = name; + this.child = child; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public Child getChild() { + return child; + } + } + + @Entity(name = "Child") + public static class Child { + @Id + private Long id; + + @NaturalId + @Column(name = "child_code") + private String code; + + private String name; + + public Child() { + } + + public Child(Long id, String code, String name) { + this.id = id; + this.code = code; + this.name = name; + } + + public Long getId() { + return id; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } + } +}