Skip to content

Commit 0b49682

Browse files
committed
HHH-18723 Support @SQLRestriction in class marked as @MappedSuperclass
1 parent e4e1b59 commit 0b49682

File tree

3 files changed

+124
-9
lines changed

3 files changed

+124
-9
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,25 @@ private static SoftDelete extractSoftDelete(ClassDetails classDetails, MetadataB
328328
if ( fromClass != null ) {
329329
return fromClass;
330330
}
331+
SoftDelete fromSuper = extractAnnotationFromMappedSuperclass( classDetails, SoftDelete.class, sourceModelContext );
332+
if ( fromSuper != null ) {
333+
return fromSuper;
334+
}
335+
return extractFromPackage( SoftDelete.class, classDetails, context );
336+
}
331337

338+
private static <A extends Annotation> A extractAnnotationFromMappedSuperclass(ClassDetails classDetails, Class<A> annotationClass, SourceModelBuildingContext sourceModelContext) {
332339
ClassDetails classToCheck = classDetails.getSuperClass();
333340
while ( classToCheck != null ) {
334-
final SoftDelete fromSuper = classToCheck.getAnnotationUsage( SoftDelete.class, sourceModelContext );
341+
final A fromSuper = classToCheck.getAnnotationUsage( annotationClass, sourceModelContext );
335342
if ( fromSuper != null
336343
&& classToCheck.hasAnnotationUsage( jakarta.persistence.MappedSuperclass.class, sourceModelContext ) ) {
337344
return fromSuper;
338345
}
339346

340347
classToCheck = classToCheck.getSuperClass();
341348
}
342-
343-
return extractFromPackage( SoftDelete.class, classDetails, context );
349+
return null;
344350
}
345351

346352
private void handleCheckConstraints() {
@@ -1627,12 +1633,21 @@ public void bindConcreteProxy() {
16271633
}
16281634

16291635
public void bindWhere() {
1630-
final SQLRestriction restriction = getOverridableAnnotation( annotatedClass, SQLRestriction.class, context );
1636+
final SQLRestriction restriction = extractSQLRestriction( annotatedClass, context );
16311637
if ( restriction != null ) {
16321638
this.where = restriction.value();
16331639
}
16341640
}
16351641

1642+
private static SQLRestriction extractSQLRestriction(ClassDetails classDetails, MetadataBuildingContext context) {
1643+
final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext();
1644+
final SQLRestriction fromClass = classDetails.getAnnotationUsage( SQLRestriction.class, sourceModelContext );
1645+
if ( fromClass != null ) {
1646+
return fromClass;
1647+
}
1648+
return extractAnnotationFromMappedSuperclass( classDetails, SQLRestriction.class, sourceModelContext );
1649+
}
1650+
16361651
public void setWrapIdsInEmbeddedComponents(boolean wrapIdsInEmbeddedComponents) {
16371652
this.wrapIdsInEmbeddedComponents = wrapIdsInEmbeddedComponents;
16381653
}

hibernate-core/src/test/java/org/hibernate/orm/test/customsql/CustomSqlRestrictionOverridesTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.security.NoSuchAlgorithmException;
2828

2929
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertNull;
3031

3132
@SessionFactory
3233
@DomainModel(annotatedClasses = CustomSqlRestrictionOverridesTest.Secure.class)
@@ -39,11 +40,16 @@
3940
public class CustomSqlRestrictionOverridesTest {
4041
@Test
4142
public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmException {
42-
Secure sec = new Secure();
43-
sec.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes());
44-
scope.inTransaction(s -> s.persist(sec) );
45-
Secure secure = scope.fromTransaction( s -> s.find( Secure.class, sec.id ) );
46-
assertNotNull(secure);
43+
Secure sec1 = new Secure();
44+
sec1.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes());
45+
scope.inTransaction(s -> s.persist(sec1) );
46+
Secure sec2 = new Secure();
47+
sec2.hash = MessageDigest.getInstance( "SHA-256" ).digest("not hello".getBytes());
48+
scope.inTransaction(s -> s.persist(sec2) );
49+
Secure secure1 = scope.fromTransaction( s -> s.find( Secure.class, sec1.id ) );
50+
assertNotNull(secure1);
51+
Secure secure2 = scope.fromTransaction( s -> s.find( Secure.class, sec2.id ) );
52+
assertNull(secure2);
4753
}
4854
@Entity
4955
@Table(name = "SecureTable")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
MappedSuperclassTest.SubClass.class
24+
}
25+
)
26+
public class MappedSuperclassTest {
27+
28+
@AfterEach
29+
public void tearDown(EntityManagerFactoryScope scope) {
30+
scope.inTransaction(
31+
entityManager -> {
32+
entityManager.createQuery( "delete from SubClass" ).executeUpdate();
33+
entityManager.createQuery( "delete from Child" ).executeUpdate();
34+
}
35+
);
36+
}
37+
38+
@Test
39+
public void testFindParent(EntityManagerFactoryScope scope) {
40+
scope.inTransaction(
41+
entityManager -> {
42+
Child child1 = new SubClass( 1L );
43+
child1.flag = true;
44+
entityManager.persist( child1 );
45+
46+
Child child2 = new Child( 2L );
47+
child2.flag = false;
48+
entityManager.persist( child2 );
49+
}
50+
);
51+
scope.inTransaction(
52+
entityManager -> {
53+
List<Child> children = entityManager.createQuery( "select c from Child c", Child.class )
54+
.getResultList();
55+
assertThat( children.size() ).isEqualTo( 1 );
56+
}
57+
);
58+
}
59+
60+
@Entity(name = "Child")
61+
public static class Child extends Intermediate {
62+
@Id
63+
private Long id;
64+
65+
public Child() {
66+
}
67+
68+
public Child(long id) {
69+
this.id = id;
70+
}
71+
}
72+
73+
@Entity(name = "SubClass")
74+
public static class SubClass extends Child {
75+
public SubClass() {
76+
}
77+
78+
public SubClass(long id) {
79+
super( id );
80+
}
81+
}
82+
83+
public static class Intermediate extends Parent {
84+
}
85+
86+
@MappedSuperclass
87+
@SQLRestriction("flag = false")
88+
public static class Parent {
89+
public Parent() {
90+
}
91+
92+
boolean flag;
93+
}
94+
}

0 commit comments

Comments
 (0)