diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryEagerAssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryEagerAssociationTest.java index 34ecb2b3d9..93db076794 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryEagerAssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryEagerAssociationTest.java @@ -14,7 +14,7 @@ import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.AfterAll; @@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; @@ -35,12 +36,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Marco Belladelli */ -@SessionFactory -@DomainModel(annotatedClasses = { +@DomainModel( annotatedClasses = { NativeQueryEagerAssociationTest.Building.class, NativeQueryEagerAssociationTest.Classroom.class -}) -@JiraKey("HHH-16191") +} ) +@SessionFactory public class NativeQueryEagerAssociationTest { @BeforeAll public void setUp(SessionFactoryScope scope) { @@ -49,15 +49,23 @@ public class NativeQueryEagerAssociationTest { final Building building2 = new Building( 2L, "building_2" ); final Building building3 = new Building( 3L, "building_3" ); final Building building4 = new Building( 4L, "building_4" ); + final Building building5 = new Building( 5L, "building_5" ); session.persist( building1 ); session.persist( building2 ); session.persist( building3 ); - session.persist( new Classroom( 1L, "classroom_1", building1, List.of( building2, building3 ) ) ); - session.persist( new Classroom( 2L, "classroom_2", building4, null ) ); + session.persist( building4 ); + session.persist( new Classroom( + 1L, + "classroom_1", + building1, + building2, + List.of( building3, building4 ) + ) ); + session.persist( new Classroom( 2L, "classroom_2", building5, null, null ) ); } ); scope.inTransaction( session -> { // delete associated entity to trigger @NotFound - session.createMutationQuery( "delete from Building where id = 4L" ).executeUpdate(); + session.createMutationQuery( "delete from Building where id = 5L" ).executeUpdate(); } ); } @@ -70,20 +78,17 @@ public class NativeQueryEagerAssociationTest { } @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-16191" ) public void testNativeQuery(SessionFactoryScope scope) { final Classroom result = scope.fromTransaction( session -> session.createNativeQuery( "select * from Classroom where id = 1", Classroom.class ) .getSingleResult() ); - assertEquals( 1L, result.getId() ); - assertTrue( Hibernate.isInitialized( result.getBuilding() ) ); - assertTrue( Hibernate.isInitialized( result.getAdjacentBuildings() ) ); - assertEquals( 1L, result.getBuilding().getId() ); - assertEquals( "building_1", result.getBuilding().getDescription() ); - assertEquals( 2, result.getAdjacentBuildings().size() ); + assertClassroom( result ); } @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-16191" ) public void testNativeQueryNotFound(SessionFactoryScope scope) { assertThrows( FetchNotFoundException.class, () -> scope.inTransaction( session -> session.createNativeQuery( "select * from Classroom where id = 2", Classroom.class ) @@ -91,8 +96,31 @@ public class NativeQueryEagerAssociationTest { ) ); } - @Entity(name = "Building") - @Table(name = "Building") + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17224" ) + public void testNativeQueryColumnAliasInjection(SessionFactoryScope scope) { + final Classroom result = (Classroom) scope.fromTransaction( + session -> session.createNativeQuery( "select {c.*} from Classroom c where c.id = 1" ) + .addEntity( "c", Classroom.class ) + .getSingleResult() + ); + assertClassroom( result ); + } + + private void assertClassroom(Classroom result) { + assertEquals( 1L, result.getId() ); + assertTrue( Hibernate.isInitialized( result.getBuilding() ) ); + assertTrue( Hibernate.isInitialized( result.getSecondaryBuilding() ) ); + assertTrue( Hibernate.isInitialized( result.getAdjacentBuildings() ) ); + assertEquals( 1L, result.getBuilding().getId() ); + assertEquals( "building_1", result.getBuilding().getDescription() ); + assertEquals( 2L, result.getSecondaryBuilding().getId() ); + assertEquals( "building_2", result.getSecondaryBuilding().getDescription() ); + assertEquals( 2, result.getAdjacentBuildings().size() ); + } + + @Entity( name = "Building" ) + @Table( name = "Building" ) public static class Building { @Id private Long id; @@ -116,28 +144,39 @@ public class NativeQueryEagerAssociationTest { } } - @Entity(name = "Classroom") - @Table(name = "Classroom") + @Entity( name = "Classroom" ) + @Table( name = "Classroom" ) public static class Classroom { @Id private Long id; private String description; - @ManyToOne(fetch = FetchType.EAGER) - @NotFound(action = NotFoundAction.EXCEPTION) + @ManyToOne( fetch = FetchType.EAGER ) + @NotFound( action = NotFoundAction.EXCEPTION ) + @JoinColumn( name = "building_id" ) private Building building; - @OneToMany(fetch = FetchType.EAGER) + @ManyToOne( fetch = FetchType.EAGER ) + @JoinColumn( name = "secondary_id" ) + private Building secondaryBuilding; + + @OneToMany( fetch = FetchType.EAGER ) private List adjacentBuildings; public Classroom() { } - public Classroom(Long id, String description, Building building, List adjacentBuildings) { + public Classroom( + Long id, + String description, + Building building, + Building secondaryBuilding, + List adjacentBuildings) { this.id = id; this.description = description; this.building = building; + this.secondaryBuilding = secondaryBuilding; this.adjacentBuildings = adjacentBuildings; } @@ -153,6 +192,10 @@ public class NativeQueryEagerAssociationTest { return building; } + public Building getSecondaryBuilding() { + return secondaryBuilding; + } + public List getAdjacentBuildings() { return adjacentBuildings; }