|
| 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.FetchType; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.JoinTable; |
| 11 | +import jakarta.persistence.ManyToOne; |
| 12 | +import org.hibernate.annotations.SQLRestriction; |
| 13 | +import org.hibernate.dialect.SybaseDialect; |
| 14 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 15 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 16 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 17 | +import org.hibernate.testing.orm.junit.Jpa; |
| 18 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 24 | + |
| 25 | +@Jpa(annotatedClasses = |
| 26 | + {ManyToOneLazyImplicitJoinTableRestrictionTest.X.class, |
| 27 | + ManyToOneLazyImplicitJoinTableRestrictionTest.Y.class}) |
| 28 | +@SkipForDialect(dialectClass = SybaseDialect.class, matchSubTypes = true, |
| 29 | + reason = "Sybase doesn't have support for upserts") |
| 30 | +@FailureExpected(jiraKey = "HHH-19555", |
| 31 | + // need to fix this using @ConcreteProxy-style lookahead |
| 32 | + reason = "restriction is not applied until entity is actually fetched") |
| 33 | +class ManyToOneLazyImplicitJoinTableRestrictionTest { |
| 34 | + @JiraKey("HHH-19555") @Test |
| 35 | + void test(EntityManagerFactoryScope scope) { |
| 36 | + scope.inTransaction( s -> { |
| 37 | + X x = new X(); |
| 38 | + Y y = new Y(); |
| 39 | + x.id = -1; |
| 40 | + y.x = x; |
| 41 | + s.persist( x ); |
| 42 | + s.persist( y ); |
| 43 | + } ); |
| 44 | + scope.inTransaction( s -> { |
| 45 | + Y y = s.find( Y.class, 0L ); |
| 46 | + y.name = "Gavin"; |
| 47 | + assertNull(y.x); |
| 48 | + } ); |
| 49 | + scope.inTransaction( s -> { |
| 50 | + Y y = s.find( Y.class, 0L ); |
| 51 | + assertEquals("Gavin", y.name); |
| 52 | + assertNull(y.x); |
| 53 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 54 | + assertEquals( -1L, id ); |
| 55 | + } ); |
| 56 | + scope.inTransaction( s -> { |
| 57 | + Y y = |
| 58 | + s.createQuery( "from Y where id = ?1", Y.class ) |
| 59 | + .setParameter( 1, 0L ) |
| 60 | + .getSingleResult(); |
| 61 | + assertEquals("Gavin", y.name); |
| 62 | + assertNull(y.x); |
| 63 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 64 | + assertEquals( -1L, id ); |
| 65 | + } ); |
| 66 | + scope.inTransaction( s -> { |
| 67 | + Y y = s.find( Y.class, 0L ); |
| 68 | + X x = new X(); |
| 69 | + x.id = 1; |
| 70 | + s.persist( x ); |
| 71 | + y.x = x; |
| 72 | + // uses a SQL merge to update the join table |
| 73 | + } ); |
| 74 | + scope.inTransaction( s -> { |
| 75 | + Y y = s.find( Y.class, 0L ); |
| 76 | + assertEquals("Gavin", y.name); |
| 77 | + assertNotNull(y.x); |
| 78 | + assertEquals( 1L, y.x.id ); |
| 79 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 80 | + assertEquals( 1L, id ); |
| 81 | + } ); |
| 82 | + scope.inTransaction( s -> { |
| 83 | + Y y = |
| 84 | + s.createQuery( "from Y where id = ?1", Y.class ) |
| 85 | + .setParameter( 1, 0L ) |
| 86 | + .getSingleResult(); |
| 87 | + assertEquals("Gavin", y.name); |
| 88 | + assertNotNull(y.x); |
| 89 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResult(); |
| 90 | + assertEquals( 1L, id ); |
| 91 | + } ); |
| 92 | + scope.inTransaction( s -> { |
| 93 | + Y y = s.find( Y.class, 0L ); |
| 94 | + y.x = null; |
| 95 | + // uses a SQL merge to update the join table |
| 96 | + } ); |
| 97 | + scope.inTransaction( s -> { |
| 98 | + Y y = s.find( Y.class, 0L ); |
| 99 | + assertEquals("Gavin", y.name); |
| 100 | + assertNull(y.x); |
| 101 | + var id = s.createNativeQuery( "select x_id from Y_X", long.class ).getSingleResultOrNull(); |
| 102 | + assertNull( id ); |
| 103 | + } ); |
| 104 | + scope.inTransaction( s -> { |
| 105 | + Y y = |
| 106 | + s.createQuery( "from Y where id = ?1", Y.class ) |
| 107 | + .setParameter( 1, 0L ) |
| 108 | + .getSingleResult(); |
| 109 | + assertEquals("Gavin", y.name); |
| 110 | + assertNull(y.x); |
| 111 | + } ); |
| 112 | + } |
| 113 | + |
| 114 | + @Entity(name="Y") |
| 115 | + static class Y { |
| 116 | + @Id |
| 117 | + long id; |
| 118 | + String name; |
| 119 | + @JoinTable |
| 120 | + @ManyToOne(fetch = FetchType.LAZY) |
| 121 | + X x; |
| 122 | + } |
| 123 | + @Entity(name="X") |
| 124 | + @SQLRestriction("id>0") |
| 125 | + static class X { |
| 126 | + @Id |
| 127 | + long id; |
| 128 | + } |
| 129 | +} |
0 commit comments