Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1627,12 +1627,30 @@ public void bindConcreteProxy() {
}

public void bindWhere() {
final SQLRestriction restriction = getOverridableAnnotation( annotatedClass, SQLRestriction.class, context );
final SQLRestriction restriction = extractSQLRestriction( annotatedClass, context );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added this test: 78cd996 to demonstrate the problem with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated your test.
Now it fails if @DialectOverride.SQLRestriction is ignored.

I will work on fixing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I solved that problem.

if ( restriction != null ) {
this.where = restriction.value();
}
}

private static SQLRestriction extractSQLRestriction(ClassDetails classDetails, MetadataBuildingContext context) {
final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext();
final SQLRestriction fromClass = getOverridableAnnotation( classDetails, SQLRestriction.class, context );
if ( fromClass != null ) {
return fromClass;
}
ClassDetails classToCheck = classDetails.getSuperClass();
while ( classToCheck != null ) {
final SQLRestriction fromSuper = getOverridableAnnotation( classToCheck, SQLRestriction.class, context );
if ( fromSuper != null
&& classToCheck.hasAnnotationUsage( jakarta.persistence.MappedSuperclass.class, sourceModelContext )) {
return fromSuper;
}
classToCheck = classToCheck.getSuperClass();
}
return null;
}

public void setWrapIdsInEmbeddedComponents(boolean wrapIdsInEmbeddedComponents) {
this.wrapIdsInEmbeddedComponents = wrapIdsInEmbeddedComponents;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.NoSuchAlgorithmException;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

@SessionFactory
@DomainModel(annotatedClasses = CustomSqlRestrictionOverridesTest.Secure.class)
Expand All @@ -39,12 +40,18 @@
public class CustomSqlRestrictionOverridesTest {
@Test
public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmException {
Secure sec = new Secure();
sec.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes());
scope.inTransaction(s -> s.persist(sec) );
Secure secure = scope.fromTransaction( s -> s.find( Secure.class, sec.id ) );
assertNotNull(secure);
Secure sec1 = new Secure();
sec1.hash = MessageDigest.getInstance( "SHA-256" ).digest( "hello".getBytes() );
scope.inTransaction( s -> s.persist( sec1 ) );
Secure sec2 = new Secure();
sec2.hash = MessageDigest.getInstance( "SHA-256" ).digest( "not hello".getBytes() );
scope.inTransaction( s -> s.persist( sec2 ) );
Secure secure1 = scope.fromTransaction( s -> s.find( Secure.class, sec1.id ) );
assertNotNull( secure1 );
Secure secure2 = scope.fromTransaction( s -> s.find( Secure.class, sec2.id ) );
assertNull( secure2 );
}

@Entity
@Table(name = "SecureTable")
@DialectOverride.SQLRestriction(dialect = H2Dialect.class,
Expand All @@ -60,7 +67,8 @@ public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmExcep
@DialectOverride.SQLRestriction(dialect = OracleDialect.class,
override = @SQLRestriction("hash = standard_hash('hello', 'SHA256')"))
static class Secure {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
byte[] hash;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.where.annotations;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@Jpa(
annotatedClasses = {
MappedSuperclassTest.Child.class,
MappedSuperclassTest.SubClass.class
}
)
public class MappedSuperclassTest {

@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from SubClass" ).executeUpdate();
entityManager.createQuery( "delete from Child" ).executeUpdate();
}
);
}

@Test
public void testFindParent(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Child child1 = new SubClass( 1L );
child1.flag = true;
entityManager.persist( child1 );

Child child2 = new Child( 2L );
child2.flag = false;
entityManager.persist( child2 );
}
);
scope.inTransaction(
entityManager -> {
List<Child> children = entityManager.createQuery( "select c from Child c", Child.class )
.getResultList();
assertThat( children.size() ).isEqualTo( 1 );
}
);
}

@Entity(name = "Child")
public static class Child extends Intermediate {
@Id
private Long id;

public Child() {
}

public Child(long id) {
this.id = id;
}
}

@Entity(name = "SubClass")
public static class SubClass extends Child {
public SubClass() {
}

public SubClass(long id) {
super( id );
}
}

public static class Intermediate extends Parent {
}

@MappedSuperclass
@SQLRestriction("flag = false")
public static class Parent {
public Parent() {
}

boolean flag;
}
}