|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 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.MappedSuperclass; |
| 10 | +import org.hibernate.annotations.SQLRestriction; |
| 11 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 12 | +import org.hibernate.testing.orm.junit.Jpa; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 19 | + |
| 20 | +@Jpa( |
| 21 | + annotatedClasses = { |
| 22 | + MappedSuperclassTest.Child.class |
| 23 | + } |
| 24 | +) |
| 25 | +public class MappedSuperclassTest { |
| 26 | + |
| 27 | + @AfterEach |
| 28 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 29 | + scope.inTransaction( |
| 30 | + entityManager -> |
| 31 | + entityManager.createQuery( "delete from Child" ).executeUpdate() |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testFindParent(EntityManagerFactoryScope scope) { |
| 37 | + scope.inTransaction( |
| 38 | + entityManager -> { |
| 39 | + Child child1 = new Child( 1L ); |
| 40 | + child1.flag = true; |
| 41 | + entityManager.persist( child1 ); |
| 42 | + |
| 43 | + Child child2 = new Child( 2L ); |
| 44 | + child2.flag = false; |
| 45 | + entityManager.persist( child2 ); |
| 46 | + } |
| 47 | + ); |
| 48 | + scope.inTransaction( |
| 49 | + entityManager -> { |
| 50 | + List<Child> children = entityManager.createQuery( "select c from Child c", Child.class ) |
| 51 | + .getResultList(); |
| 52 | + assertThat( children.size() ).isEqualTo( 1 ); |
| 53 | + } |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @Entity(name = "Child") |
| 58 | + public static class Child extends Intermediate { |
| 59 | + @Id |
| 60 | + private Long id; |
| 61 | + |
| 62 | + public Child() { |
| 63 | + } |
| 64 | + |
| 65 | + public Child(long id) { |
| 66 | + this.id = id; |
| 67 | + } |
| 68 | + } |
| 69 | + public static class Intermediate extends Parent { |
| 70 | + } |
| 71 | + |
| 72 | + @MappedSuperclass |
| 73 | + @SQLRestriction("flag = false") |
| 74 | + public static class Parent { |
| 75 | + public Parent() { |
| 76 | + } |
| 77 | + |
| 78 | + boolean flag; |
| 79 | + } |
| 80 | +} |
0 commit comments