Skip to content

Commit cce466e

Browse files
committed
HHH-15229 Add test for issue
1 parent 64c3969 commit cce466e

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
package org.hibernate.orm.test.notfound.exception;
2+
3+
import java.util.Set;
4+
5+
import org.hibernate.FetchNotFoundException;
6+
import org.hibernate.annotations.NotFound;
7+
import org.hibernate.annotations.NotFoundAction;
8+
9+
import org.hibernate.testing.TestForIssue;
10+
import org.hibernate.testing.orm.junit.DomainModel;
11+
import org.hibernate.testing.orm.junit.SessionFactory;
12+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
16+
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.FetchType;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.GenerationType;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.JoinColumn;
23+
import jakarta.persistence.ManyToOne;
24+
import jakarta.persistence.OneToMany;
25+
import jakarta.persistence.Table;
26+
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertNull;
29+
30+
@DomainModel(
31+
annotatedClasses = {
32+
NotFoundExceptionTest.ChessGame.class,
33+
NotFoundExceptionTest.ChessPlayer.class
34+
}
35+
)
36+
@SessionFactory
37+
@TestForIssue(jiraKey = "HHH-15229")
38+
public class NotFoundExceptionTest {
39+
40+
@AfterEach
41+
public void setUp(SessionFactoryScope scope) {
42+
scope.inTransaction(
43+
session ->
44+
session.createNativeMutationQuery( "delete from CHESS_GAME" ).executeUpdate()
45+
);
46+
}
47+
48+
@Test
49+
public void testNotFoundIgnore(SessionFactoryScope scope) {
50+
scope.inTransaction(
51+
session ->
52+
session.createNativeMutationQuery(
53+
"INSERT INTO CHESS_GAME(id, player_black_id) VALUES (1, 1)" )
54+
.executeUpdate()
55+
56+
);
57+
58+
scope.inTransaction(
59+
session -> {
60+
ChessGame game = session.find( ChessGame.class, 1L );
61+
assertNotNull( game, "Returned entity shouldn't be null" );
62+
assertNull(
63+
game.getPlayerBlack(),
64+
"Broken foreign key reference with NotFoundAction.IGNORE should return null"
65+
);
66+
assertNull( game.getPlayerWhite() );
67+
}
68+
);
69+
70+
scope.inTransaction(
71+
session -> {
72+
ChessGame chessGame = session.getReference( ChessGame.class, 1L );
73+
assertNotNull( chessGame );
74+
assertNull(
75+
chessGame.getPlayerBlack(),
76+
"Broken foreign key reference with NotFoundAction.IGNORE should return null"
77+
);
78+
assertNull( chessGame.getPlayerWhite() );
79+
}
80+
);
81+
}
82+
83+
@Test
84+
public void testNotFoundIgnoreAndNotFoundException(SessionFactoryScope scope) {
85+
scope.inTransaction(
86+
session ->
87+
session.createNativeMutationQuery(
88+
"INSERT INTO CHESS_GAME(id, player_white_id, player_black_id) VALUES (1, 1, 2)" )
89+
.executeUpdate()
90+
);
91+
92+
Assertions.assertThrows(
93+
FetchNotFoundException.class, () -> scope.inTransaction(
94+
session ->
95+
session.find( ChessGame.class, 1L )
96+
)
97+
);
98+
99+
Assertions.assertThrows(
100+
FetchNotFoundException.class, () -> scope.inTransaction(
101+
session -> {
102+
ChessGame chessGame = session.getReference( ChessGame.class, 1L );
103+
assertNotNull( chessGame );
104+
// this triggers ChessGame initialization, and because playerBlack cannot be found the FetchNotFoundException is thown
105+
assertNotNull( chessGame.getPlayerBlack() );
106+
}
107+
)
108+
);
109+
}
110+
111+
@Test
112+
public void testNotFoundException(SessionFactoryScope scope) {
113+
scope.inTransaction(
114+
session ->
115+
session.createNativeMutationQuery(
116+
"INSERT INTO CHESS_GAME(id, player_white_id) VALUES (1, 1)" )
117+
.executeUpdate()
118+
);
119+
120+
Assertions.assertThrows(
121+
FetchNotFoundException.class, () -> scope.inTransaction(
122+
session ->
123+
session.find( ChessGame.class, 1L )
124+
)
125+
);
126+
127+
Assertions.assertThrows(
128+
FetchNotFoundException.class, () -> scope.inTransaction(
129+
session -> {
130+
ChessGame chessGame = session.getReference( ChessGame.class, 1L );
131+
assertNotNull( chessGame );
132+
// this triggers ChessGame initialization, and because playerBlack cannot be found the FetchNotFoundException is thown
133+
assertNotNull( chessGame.getPlayerBlack() );
134+
}
135+
)
136+
);
137+
}
138+
139+
@Entity(name = "ChessPlayer")
140+
@Table(name = "CHESS_PLAYER")
141+
public static class ChessPlayer {
142+
143+
@Id
144+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
145+
private Long id;
146+
147+
private String name;
148+
149+
@OneToMany(mappedBy = "playerWhite")
150+
private Set<ChessGame> gamesWhite;
151+
152+
@OneToMany(mappedBy = "playerBlack")
153+
private Set<ChessGame> gamesBlack;
154+
155+
public Long getId() {
156+
return id;
157+
}
158+
159+
public void setId(Long id) {
160+
this.id = id;
161+
}
162+
163+
public String getName() {
164+
return name;
165+
}
166+
167+
public void setName(String name) {
168+
this.name = name;
169+
}
170+
171+
public Set<ChessGame> getGamesWhite() {
172+
return gamesWhite;
173+
}
174+
175+
public void setGamesWhite(Set<ChessGame> gamesWhite) {
176+
this.gamesWhite = gamesWhite;
177+
}
178+
179+
public Set<ChessGame> getGamesBlack() {
180+
return gamesBlack;
181+
}
182+
183+
public void setGamesBlack(Set<ChessGame> gamesBlack) {
184+
this.gamesBlack = gamesBlack;
185+
}
186+
}
187+
188+
@Entity(name = "ChessGame")
189+
@Table(name = "CHESS_GAME")
190+
public static class ChessGame {
191+
192+
@Id
193+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
194+
private Long id;
195+
196+
private String name;
197+
198+
@ManyToOne(fetch = FetchType.LAZY)
199+
@JoinColumn(name = "player_white_id")
200+
@NotFound(action = NotFoundAction.EXCEPTION)
201+
private ChessPlayer playerWhite;
202+
203+
@ManyToOne(fetch = FetchType.LAZY)
204+
@JoinColumn(name = "player_black_id")
205+
@NotFound(action = NotFoundAction.IGNORE)
206+
private ChessPlayer playerBlack;
207+
208+
public Long getId() {
209+
return id;
210+
}
211+
212+
public void setId(Long id) {
213+
this.id = id;
214+
}
215+
216+
public String getName() {
217+
return name;
218+
}
219+
220+
public void setName(String name) {
221+
this.name = name;
222+
}
223+
224+
public ChessPlayer getPlayerWhite() {
225+
return playerWhite;
226+
}
227+
228+
public void setPlayerWhite(ChessPlayer playerWhite) {
229+
this.playerWhite = playerWhite;
230+
}
231+
232+
public ChessPlayer getPlayerBlack() {
233+
return playerBlack;
234+
}
235+
236+
public void setPlayerBlack(ChessPlayer playerBlack) {
237+
this.playerBlack = playerBlack;
238+
}
239+
}
240+
}

0 commit comments

Comments
 (0)