diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/MultiNaturalIdLoaderStandard.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/MultiNaturalIdLoaderStandard.java index afb2d60418..7a39df3913 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/MultiNaturalIdLoaderStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/MultiNaturalIdLoaderStandard.java @@ -84,6 +84,10 @@ public class MultiNaturalIdLoaderStandard implements MultiNaturalIdLoader final List results = batcher.multiLoad( naturalIds, options, session ); + if ( results.size() == 1 ) { + return results; + } + if ( options.isOrderReturnEnabled() ) { throw new NotYetImplementedFor6Exception( getClass() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/loading/multiLoad/MultiLoadSingleEventTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/loading/multiLoad/MultiLoadSingleEventTest.java index 603f482a2e..bf3c78c77a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/loading/multiLoad/MultiLoadSingleEventTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/loading/multiLoad/MultiLoadSingleEventTest.java @@ -8,6 +8,8 @@ package org.hibernate.orm.test.loading.multiLoad; import java.util.List; +import org.hibernate.annotations.NaturalId; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -20,15 +22,15 @@ public class MultiLoadSingleEventTest extends BaseCoreFunctionalTestCase { @Override protected Class[] getAnnotatedClasses() { - return new Class[] { Event.class }; + return new Class[] { IdEvent.class, NaturalIdEvent.class }; } @Test - public void test() { - inTransaction( session -> session.persist( new Event( 1 ) ) ); + public void byId() { + inTransaction( session -> session.persist( new IdEvent( 1 ) ) ); inTransaction( session -> { - List events = session.byMultipleIds( Event.class ) + List events = session.byMultipleIds( IdEvent.class ) .multiLoad( 1 ); assertThat( events ).hasSize( 1 ); @@ -37,18 +39,32 @@ public class MultiLoadSingleEventTest extends BaseCoreFunctionalTestCase { } ); } - @Entity(name = "Event") - public static class Event { + @Test + public void byNaturalId() { + inTransaction( session -> session.persist( new NaturalIdEvent( 1 ) ) ); + + inTransaction( session -> { + List events = session.byMultipleNaturalId( NaturalIdEvent.class ) + .multiLoad( "code1" ); + + assertThat( events ).hasSize( 1 ); + assertThat( events.get( 0 ) ).isNotNull(); + assertThat( events ).extracting( "id" ).containsExactly( 1 ); + } ); + } + + @Entity(name = "IdEvent") + public static class IdEvent { @Id private Integer id; private String text; - public Event() { + public IdEvent() { } - public Event(Integer id) { + public IdEvent(Integer id) { this.id = id; this.text = "text" + id; } @@ -69,4 +85,38 @@ public class MultiLoadSingleEventTest extends BaseCoreFunctionalTestCase { this.text = text; } } + + @Entity(name = "NaturalIdEvent") + public static class NaturalIdEvent { + + @Id + private Integer id; + + @NaturalId + private String code; + + public NaturalIdEvent() { + } + + public NaturalIdEvent(Integer id) { + this.id = id; + this.code = "code" + id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + } }