diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/Speech.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/Speech.java index 1b08f7a204..1ea236ee6d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/Speech.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/Speech.java @@ -5,34 +5,46 @@ package org.hibernate.orm.test.sql.hand; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + /** * @author Emmanuel Bernard */ -public class Speech { +@Entity +public class Speech implements SpeechInterface { + @Id @GeneratedValue private Integer id; private String name; private Double length; + @Override public Integer getId() { return id; } + @Override public void setId(Integer id) { this.id = id; } + @Override public Double getLength() { return length; } + @Override public void setLength(Double length) { this.length = length; } + @Override public String getName() { return name; } + @Override public void setName(String name) { this.name = name; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/SpeechInterface.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/SpeechInterface.java new file mode 100644 index 0000000000..c238298545 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/SpeechInterface.java @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.sql.hand; + +/** + * @author Steve Ebersole + */ +public interface SpeechInterface { + Integer getId(); + void setId(Integer id); + + Double getLength(); + void setLength(Double length); + + String getName(); + void setName(String name); +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/query/EntityReturnClassTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/query/EntityReturnClassTests.java new file mode 100644 index 0000000000..88f8c47a61 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/query/EntityReturnClassTests.java @@ -0,0 +1,131 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.sql.hand.query; + +import org.hibernate.orm.test.sql.hand.Speech; +import org.hibernate.orm.test.sql.hand.SpeechInterface; +import org.hibernate.query.NativeQuery; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.NotImplementedYet; +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 java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Steve Ebersole + */ +@SuppressWarnings("JUnitMalformedDeclaration") +@DomainModel(annotatedClasses = {SpeechInterface.class, Speech.class}) +@SessionFactory +@Jira("https://hibernate.atlassian.net/browse/HHH-18864") +public class EntityReturnClassTests { + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // no return-class -> no problem + + @Test + @JiraKey("HHH-18864") + public void testAddEntityNoReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + //noinspection unchecked,deprecation + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s" ); + query.addEntity("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + @Test + @JiraKey("HHH-18864") + public void testAddRootNoReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + //noinspection unchecked,deprecation + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s" ); + query.addRoot("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // entity return-class -> problems with 2 `ResultBuilder` refs + + @Test + @JiraKey("HHH-18864") + @NotImplementedYet + public void testAddEntityWithEntityReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s", Speech.class ); + query.addEntity("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + @Test + @JiraKey("HHH-18864") + @NotImplementedYet + public void testAddRootWithEntityReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s", Speech.class ); + query.addRoot("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // entity-interface return-class -> problems with `JdbcType` determination + + @Test + @JiraKey("HHH-18864") + @NotImplementedYet + public void testAddEntityWithInterfaceReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s", SpeechInterface.class ); + query.addEntity("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + @Test + @JiraKey("HHH-18864") + @NotImplementedYet + public void testAddRootWithInterfaceReturn(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + NativeQuery query = session.createNativeQuery( "select {s.*} from Speech s", SpeechInterface.class ); + query.addRoot("s", Speech.class); + List l = query.list(); + assertEquals( l.size(), 1 ); + } ); + } + + + @BeforeEach + void prepareTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> { + Speech speech = new Speech(); + speech.setLength( 23d ); + speech.setName( "Mine" ); + session.persist( speech ); + } ); + } + + @AfterEach + void dropTestData(SessionFactoryScope scope) { + scope.inTransaction( (session) -> session.createMutationQuery( "delete Speech" ).executeUpdate() ); + } +}