1111import jakarta .persistence .Table ;
1212import org .hibernate .annotations .SQLRestriction ;
1313import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
14+ import org .hibernate .testing .orm .junit .JiraKey ;
1415import org .hibernate .testing .orm .junit .Jpa ;
1516import org .junit .jupiter .api .Test ;
1617
18+ import static org .junit .jupiter .api .Assertions .assertEquals ;
1719import static org .junit .jupiter .api .Assertions .assertNotNull ;
18- import static org .junit .jupiter .api .AssertionsKt .assertNull ;
1920
2021@ Jpa (annotatedClasses = {ManyToOneRestrictionTest .X .class , ManyToOneRestrictionTest .Y .class })
2122class ManyToOneRestrictionTest {
23+ @ JiraKey ("HHH-19565" )
2224 @ Test void test (EntityManagerFactoryScope scope ) {
2325 scope .inTransaction (em -> {
2426 Y y = new Y ();
@@ -28,24 +30,22 @@ class ManyToOneRestrictionTest {
2830 em .persist (x );
2931 em .persist (y );
3032 });
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
3137 scope .inTransaction (em -> {
3238 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 );
3841 y .name = "hello" ;
3942 });
4043 scope .inTransaction (em -> {
4144 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 );
4748 });
48-
4949 }
5050
5151 @ Entity
0 commit comments