Skip to content

Commit

Permalink
Support multiload by single natural id
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and sebersole committed Dec 15, 2021
1 parent 2814c05 commit 28b8b33
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public <K> List<E> multiLoad(K[] naturalIds, MultiNaturalIdLoadOptions options,

final List<E> results = batcher.multiLoad( naturalIds, options, session );

if ( results.size() == 1 ) {
return results;
}

if ( options.isOrderReturnEnabled() ) {
throw new NotYetImplementedFor6Exception( getClass() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.util.List;

import org.hibernate.annotations.NaturalId;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

Expand All @@ -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<Event> events = session.byMultipleIds( Event.class )
List<IdEvent> events = session.byMultipleIds( IdEvent.class )
.multiLoad( 1 );

assertThat( events ).hasSize( 1 );
Expand All @@ -37,18 +39,32 @@ public void test() {
} );
}

@Entity(name = "Event")
public static class Event {
@Test
public void byNaturalId() {
inTransaction( session -> session.persist( new NaturalIdEvent( 1 ) ) );

inTransaction( session -> {
List<NaturalIdEvent> 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;
}
Expand All @@ -69,4 +85,38 @@ public void setText(String text) {
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;
}
}
}

0 comments on commit 28b8b33

Please sign in to comment.