|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.manytoone.jointable; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.JoinTable; |
| 10 | +import jakarta.persistence.ManyToOne; |
| 11 | +import org.hibernate.annotations.SQLRestriction; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | + |
| 21 | +@Jpa(annotatedClasses = |
| 22 | + {ManyToOneImplicitJoinTableRestrictionTest.X.class, |
| 23 | + ManyToOneImplicitJoinTableRestrictionTest.Y.class}) |
| 24 | +class ManyToOneImplicitJoinTableRestrictionTest { |
| 25 | + @JiraKey("HHH-19555") @Test |
| 26 | + void test(EntityManagerFactoryScope scope) { |
| 27 | + scope.inTransaction( s -> { |
| 28 | + X x = new X(); |
| 29 | + Y y = new Y(); |
| 30 | + x.id = -1; |
| 31 | + y.x = x; |
| 32 | + s.persist( x ); |
| 33 | + s.persist( y ); |
| 34 | + } ); |
| 35 | + scope.inTransaction( s -> { |
| 36 | + Y y = s.find( Y.class, 0L ); |
| 37 | + y.name = "Gavin"; |
| 38 | + assertNull(y.x); |
| 39 | + } ); |
| 40 | + scope.inTransaction( s -> { |
| 41 | + Y y = s.find( Y.class, 0L ); |
| 42 | + assertEquals("Gavin", y.name); |
| 43 | + assertNull(y.x); |
| 44 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 45 | + assertEquals( -1L, id ); |
| 46 | + } ); |
| 47 | + scope.inTransaction( s -> { |
| 48 | + Y y = s.find( Y.class, 0L ); |
| 49 | + X x = new X(); |
| 50 | + x.id = 1; |
| 51 | + s.persist( x ); |
| 52 | + y.x = x; |
| 53 | + // uses a SQL merge to update the join table |
| 54 | + } ); |
| 55 | + scope.inTransaction( s -> { |
| 56 | + Y y = s.find( Y.class, 0L ); |
| 57 | + assertEquals("Gavin", y.name); |
| 58 | + assertNotNull(y.x); |
| 59 | + assertEquals( 1L, y.x.id ); |
| 60 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 61 | + assertEquals( 1L, id ); |
| 62 | + } ); |
| 63 | + scope.inTransaction( s -> { |
| 64 | + Y y = s.find( Y.class, 0L ); |
| 65 | + y.x = null; |
| 66 | + // uses a SQL merge to update the join table |
| 67 | + } ); |
| 68 | + scope.inTransaction( s -> { |
| 69 | + Y y = s.find( Y.class, 0L ); |
| 70 | + assertEquals("Gavin", y.name); |
| 71 | + assertNull(y.x); |
| 72 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResultOrNull(); |
| 73 | + assertNull( id ); |
| 74 | + } ); |
| 75 | + } |
| 76 | + |
| 77 | + @Entity(name="Y") |
| 78 | + static class Y { |
| 79 | + @Id |
| 80 | + long id; |
| 81 | + String name; |
| 82 | + @JoinTable |
| 83 | + @ManyToOne X x; |
| 84 | + } |
| 85 | + @Entity(name="X") |
| 86 | + @SQLRestriction("id>0") |
| 87 | + static class X { |
| 88 | + @Id |
| 89 | + long id; |
| 90 | + } |
| 91 | +} |
0 commit comments