-
Notifications
You must be signed in to change notification settings - Fork 92
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
hibernate-reactive-core/src/test/java/org/hibernate/reactive/ManyToOneIdClassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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() { | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missing Override annotation Note test