Skip to content

Commit dbea9de

Browse files
dreab8DavideD
authored andcommitted
[#2007] Add test for NPE retrieving Entity with composite id mixing ManyToOne with basic attributes
1 parent 9224ce8 commit dbea9de

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.util.Collection;
9+
import java.util.List;
10+
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import io.vertx.junit5.Timeout;
16+
import io.vertx.junit5.VertxTestContext;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.FetchType;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.IdClass;
21+
import jakarta.persistence.ManyToOne;
22+
23+
import static java.util.concurrent.TimeUnit.MINUTES;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
@Timeout(value = 1, timeUnit = MINUTES)
27+
public class ManyToOneIdClassTest extends BaseReactiveTest {
28+
29+
private final static String USER_NAME = "user";
30+
private final static String SUBSYSTEM_ID = "1";
31+
32+
@Override
33+
protected Collection<Class<?>> annotatedEntities() {
34+
return List.of( SystemUser.class, Subsystem.class );
35+
}
36+
37+
@BeforeEach
38+
public void populateDb(VertxTestContext context) {
39+
Subsystem subsystem = new Subsystem( SUBSYSTEM_ID, "sub 1" );
40+
SystemUser systemUser = new SystemUser( subsystem, USER_NAME, "system 1" );
41+
test(
42+
context, getMutinySessionFactory()
43+
.withTransaction( (s, t) -> s.persistAll( subsystem, systemUser ) )
44+
);
45+
}
46+
47+
@AfterEach
48+
public void after(VertxTestContext context) {
49+
test( context, cleanDb() );
50+
}
51+
52+
@Test
53+
public void testQuery(VertxTestContext context) {
54+
test(
55+
context, openSession()
56+
.thenAccept( session -> session.createQuery( "SELECT s FROM SystemUser s", SystemUser.class )
57+
.getResultList().thenAccept( list -> {
58+
assertThat( list.size() ).isEqualTo( 1 );
59+
SystemUser systemUser = list.get( 0 );
60+
assertThat( systemUser.getSubsystem().getId() ).isEqualTo( SUBSYSTEM_ID );
61+
assertThat( systemUser.getUsername() ).isEqualTo( USER_NAME );
62+
} ) )
63+
);
64+
}
65+
66+
@Entity(name = "SystemUser")
67+
@IdClass(PK.class)
68+
public static class SystemUser {
69+
70+
@Id
71+
@ManyToOne(fetch = FetchType.LAZY)
72+
private Subsystem subsystem;
73+
74+
@Id
75+
private String username;
76+
77+
private String name;
78+
79+
public SystemUser() {
80+
}
81+
82+
public SystemUser(Subsystem subsystem, String username, String name) {
83+
this.subsystem = subsystem;
84+
this.username = username;
85+
this.name = name;
86+
}
87+
88+
public Subsystem getSubsystem() {
89+
return subsystem;
90+
}
91+
92+
public String getUsername() {
93+
return username;
94+
}
95+
96+
public String getName() {
97+
return name;
98+
}
99+
}
100+
101+
@Entity(name = "Subsystem")
102+
public static class Subsystem {
103+
104+
@Id
105+
private String id;
106+
107+
private String description;
108+
109+
public Subsystem() {
110+
}
111+
112+
public Subsystem(String id, String description) {
113+
this.id = id;
114+
this.description = description;
115+
}
116+
117+
public String getId() {
118+
return id;
119+
}
120+
121+
public String getDescription() {
122+
return description;
123+
}
124+
}
125+
126+
public static class PK {
127+
128+
private Subsystem subsystem;
129+
130+
private String username;
131+
132+
public PK(Subsystem subsystem, String username) {
133+
this.subsystem = subsystem;
134+
this.username = username;
135+
}
136+
137+
private PK() {
138+
}
139+
}
140+
141+
}

0 commit comments

Comments
 (0)