Skip to content

Commit c3d61f0

Browse files
committed
HHH-18723 Support @SQLRestriction in class marked as @MappedSuperclass
1 parent bcac1e3 commit c3d61f0

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/SQLRestriction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.annotations;
66

7+
import java.lang.annotation.Inherited;
78
import java.lang.annotation.Retention;
89
import java.lang.annotation.Target;
910

@@ -62,6 +63,7 @@
6263
*/
6364
@Target({TYPE, METHOD, FIELD})
6465
@Retention(RUNTIME)
66+
@Inherited
6567
public @interface SQLRestriction {
6668
/**
6769
* A predicate, written in native SQL.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)