|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.where.annotations; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.JoinColumn; |
| 10 | +import jakarta.persistence.ManyToOne; |
| 11 | +import jakarta.persistence.Table; |
| 12 | +import org.hibernate.annotations.SQLRestriction; |
| 13 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 18 | +import static org.junit.jupiter.api.AssertionsKt.assertNull; |
| 19 | + |
| 20 | +@Jpa(annotatedClasses = {ManyToOneRestrictionTest.X.class, ManyToOneRestrictionTest.Y.class}) |
| 21 | +class ManyToOneRestrictionTest { |
| 22 | + @Test void test(EntityManagerFactoryScope scope) { |
| 23 | + scope.inTransaction(em -> { |
| 24 | + Y y = new Y(); |
| 25 | + X x = new X(); |
| 26 | + x.id = -1; |
| 27 | + y.x = x; |
| 28 | + em.persist(x); |
| 29 | + em.persist(y); |
| 30 | + }); |
| 31 | + scope.inTransaction(em -> { |
| 32 | + 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); |
| 38 | + y.name = "hello"; |
| 39 | + }); |
| 40 | + scope.inTransaction(em -> { |
| 41 | + 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); |
| 47 | + }); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Entity |
| 52 | + @Table(name = "XX") |
| 53 | + @SQLRestriction("id>0") |
| 54 | + static class X { |
| 55 | + @Id |
| 56 | + long id; |
| 57 | + } |
| 58 | + @Entity |
| 59 | + @Table(name = "YY") |
| 60 | + static class Y { |
| 61 | + @Id |
| 62 | + long id; |
| 63 | + String name; |
| 64 | + @ManyToOne |
| 65 | + @JoinColumn(name = "xx") |
| 66 | + X x; |
| 67 | + } |
| 68 | +} |
0 commit comments