Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2007] NPE retrieving Entity with composite id mixing ManyToOne with basic attributes #2057

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CompletionStage<Void> reactiveResolveKey(NonAggregatedIdentifierMappingIn
final RowProcessingState rowProcessingState = data.getRowProcessingState();
final boolean[] dataIsMissing = {false};
return loop( getInitializers(), initializer -> {
if ( dataIsMissing[0] ) {
if ( dataIsMissing[0] || initializer == null ) {
return voidFuture();
}
final InitializerData subData = ( (ReactiveInitializer<?>) initializer )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive;

import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.ManyToOne;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;

@Timeout(value = 1, timeUnit = MINUTES)
public class ManyToOneIdClassTest extends BaseReactiveTest {

private final static String USER_NAME = "user";
private final static String SUBSYSTEM_ID = "1";

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( SystemUser.class, Subsystem.class );
}

@BeforeEach
public void populateDb(VertxTestContext context) {
Subsystem subsystem = new Subsystem( SUBSYSTEM_ID, "sub 1" );
SystemUser systemUser = new SystemUser( subsystem, USER_NAME, "system 1" );
test(
context, getMutinySessionFactory()
.withTransaction( (s, t) -> s.persistAll( subsystem, systemUser ) )
);
}

@AfterEach
public void after(VertxTestContext context) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note test

This method overrides
BaseReactiveTest.after
; it is advisable to add an Override annotation.
test( context, cleanDb() );
}

@Test
public void testQuery(VertxTestContext context) {
test(
context, openSession()
.thenAccept( session -> session.createQuery( "SELECT s FROM SystemUser s", SystemUser.class )
.getResultList().thenAccept( list -> {
assertThat( list.size() ).isEqualTo( 1 );
SystemUser systemUser = list.get( 0 );
assertThat( systemUser.getSubsystem().getId() ).isEqualTo( SUBSYSTEM_ID );
assertThat( systemUser.getUsername() ).isEqualTo( USER_NAME );
} ) )
);
}

@Entity(name = "SystemUser")
@IdClass(PK.class)
public static class SystemUser {

@Id
@ManyToOne(fetch = FetchType.LAZY)
private Subsystem subsystem;

@Id
private String username;

private String name;

public SystemUser() {
}

public SystemUser(Subsystem subsystem, String username, String name) {
this.subsystem = subsystem;
this.username = username;
this.name = name;
}

public Subsystem getSubsystem() {
return subsystem;
}

public String getUsername() {
return username;
}

public String getName() {
return name;
}
}

@Entity(name = "Subsystem")
public static class Subsystem {

@Id
private String id;

private String description;

public Subsystem() {
}

public Subsystem(String id, String description) {
this.id = id;
this.description = description;
}

public String getId() {
return id;
}

public String getDescription() {
return description;
}
}

public static class PK {

private Subsystem subsystem;

private String username;

public PK(Subsystem subsystem, String username) {
this.subsystem = subsystem;
this.username = username;
}

private PK() {
}
}

}
Loading