11
11
import jakarta .persistence .Table ;
12
12
import org .hibernate .annotations .SQLRestriction ;
13
13
import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
14
+ import org .hibernate .testing .orm .junit .JiraKey ;
14
15
import org .hibernate .testing .orm .junit .Jpa ;
15
16
import org .junit .jupiter .api .Test ;
16
17
18
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
17
19
import static org .junit .jupiter .api .Assertions .assertNotNull ;
18
- import static org .junit .jupiter .api .AssertionsKt .assertNull ;
19
20
20
21
@ Jpa (annotatedClasses = {ManyToOneRestrictionTest .X .class , ManyToOneRestrictionTest .Y .class })
21
22
class ManyToOneRestrictionTest {
23
+ @ JiraKey ("HHH-19565" )
22
24
@ Test void test (EntityManagerFactoryScope scope ) {
23
25
scope .inTransaction (em -> {
24
26
Y y = new Y ();
@@ -28,24 +30,22 @@ class ManyToOneRestrictionTest {
28
30
em .persist (x );
29
31
em .persist (y );
30
32
});
33
+ // @SQLRestrictions should not be applied to
34
+ // foreign key associations, or the FK will
35
+ // be set to null when the entity is updated,
36
+ // leading to data loss
31
37
scope .inTransaction (em -> {
32
38
Y y = em .find (Y .class , 0L );
33
- assertNull (y .x );
34
- var fk =
35
- em .createNativeQuery ( "select xx from YY" , long .class )
36
- .getSingleResultOrNull ();
37
- assertNotNull (fk );
39
+ assertNotNull (y .x );
40
+ assertEquals (-1 , y .x .id );
38
41
y .name = "hello" ;
39
42
});
40
43
scope .inTransaction (em -> {
41
44
Y y = em .find (Y .class , 0L );
42
- assertNull (y .x );
43
- var fk =
44
- em .createNativeQuery ( "select xx from YY" , long .class )
45
- .getSingleResultOrNull ();
46
- assertNotNull (fk );
45
+ assertNotNull (y .x );
46
+ assertEquals (-1 , y .x .id );
47
+ assertEquals ("hello" , y .name );
47
48
});
48
-
49
49
}
50
50
51
51
@ Entity
0 commit comments