Skip to content

Commit b99938d

Browse files
committed
HHH-18979 provide untypesafe version of API
1 parent cdf2c94 commit b99938d

File tree

11 files changed

+93
-0
lines changed

11 files changed

+93
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query;
6+
7+
import jakarta.persistence.criteria.CriteriaBuilder;
8+
import jakarta.persistence.criteria.Predicate;
9+
import jakarta.persistence.criteria.Root;
10+
import jakarta.persistence.metamodel.Attribute;
11+
import jakarta.persistence.metamodel.EntityType;
12+
import jakarta.persistence.metamodel.SingularAttribute;
13+
import org.hibernate.query.range.Range;
14+
15+
/**
16+
* Restricts an attribute of an entity to a given {@link Range}.
17+
*
18+
* @param <X> The entity type
19+
* @param <U> The attribute type
20+
*/
21+
record NamedAttributeRange<X, U>(Class<X> entity, String attributeName, Range<U> range) implements Restriction<X> {
22+
@Override
23+
public Restriction<X> negated() {
24+
return new Negation<>( this );
25+
}
26+
27+
@Override
28+
public Predicate toPredicate(Root<? extends X> root, CriteriaBuilder builder) {
29+
final EntityType<? extends X> entityType = root.getModel();
30+
if ( !entity.isAssignableFrom( entityType.getJavaType() ) ) {
31+
throw new IllegalArgumentException( "Root entity is not a subtype of '" + entity.getTypeName() + "'" );
32+
}
33+
final Attribute<?, ?> att = entityType.getAttribute( attributeName );
34+
if ( !range.getType().isAssignableFrom( att.getJavaType() ) ) {
35+
throw new IllegalArgumentException( "Attribute '" + attributeName
36+
+ "' is not assignable to range of type '" + range.getType().getName() + "'" );
37+
}
38+
if ( !(att instanceof SingularAttribute) ) {
39+
throw new IllegalArgumentException( "Attribute '" + attributeName + "' is not singular" );
40+
}
41+
@SuppressWarnings("unchecked")
42+
final SingularAttribute<X, U> attribute = (SingularAttribute<X, U>) att;
43+
return range.toPredicate( root, attribute, builder );
44+
}
45+
}

hibernate-core/src/main/java/org/hibernate/query/Restriction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static <T, U> Restriction<T> restrict(SingularAttribute<T, U> attribute, Range<U
5757
return new AttributeRange<>( attribute, range );
5858
}
5959

60+
static <T> Restriction<T> restrict(Class<T> type, String attributeName, Range<?> range) {
61+
return new NamedAttributeRange<>( type, attributeName, range );
62+
}
63+
6064
static <T, U> Restriction<T> equal(SingularAttribute<T, U> attribute, U value) {
6165
return restrict( attribute, Range.singleValue( value ) );
6266
}

hibernate-core/src/main/java/org/hibernate/query/range/CaseInsensitiveValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, St
2525
final Expression<String> literal = builder.literal( value.toLowerCase( Locale.ROOT ) );
2626
return builder.lower( root.get( attribute ) ).equalTo( literal );
2727
}
28+
29+
@Override
30+
public Class<String> getType() {
31+
return String.class;
32+
}
2833
}

hibernate-core/src/main/java/org/hibernate/query/range/Interval.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, U>
2222
: builder.between( root.get( attribute ),
2323
builder.literal( lowerBound.bound() ), builder.literal( upperBound.bound() ) );
2424
}
25+
26+
@Override
27+
public Class<? extends U> getType() {
28+
return lowerBound.getType();
29+
}
2530
}

hibernate-core/src/main/java/org/hibernate/query/range/LowerBound.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, U>
2525
? builder.greaterThan( root.get( attribute ), literal )
2626
: builder.greaterThanOrEqualTo( root.get( attribute ), literal );
2727
}
28+
29+
@Override @SuppressWarnings("unchecked")
30+
public Class<? extends U> getType() {
31+
return (Class<? extends U>) bound.getClass();
32+
}
2833
}

hibernate-core/src/main/java/org/hibernate/query/range/Pattern.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, St
2222
: builder.like( builder.lower( root.get( attribute ) ),
2323
builder.literal( pattern.toLowerCase( Locale.ROOT ) ) );
2424
}
25+
26+
@Override
27+
public Class<String> getType() {
28+
return String.class;
29+
}
2530
}

hibernate-core/src/main/java/org/hibernate/query/range/Range.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
@Incubating
2929
public interface Range<U> {
3030

31+
Class<? extends U> getType();
32+
3133
/**
3234
* Return a JPA Criteria {@link Predicate} constraining the given
3335
* attribute of the given root entity to this domain of allowed

hibernate-core/src/main/java/org/hibernate/query/range/UpperBound.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, U>
2525
? builder.lessThan( root.get( attribute ), literal )
2626
: builder.lessThanOrEqualTo( root.get( attribute ), literal );
2727
}
28+
29+
@Override @SuppressWarnings("unchecked")
30+
public Class<? extends U> getType() {
31+
return (Class<? extends U>) bound.getClass();
32+
}
2833
}

hibernate-core/src/main/java/org/hibernate/query/range/Value.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, U>
2323
final Expression<U> literal = builder.literal( value );
2424
return root.get( attribute ).equalTo( literal );
2525
}
26+
27+
@Override @SuppressWarnings("unchecked")
28+
public Class<? extends U> getType() {
29+
return (Class<? extends U>) value.getClass();
30+
}
2631
}

hibernate-core/src/main/java/org/hibernate/query/range/ValueList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ record ValueList<U>(List<U> values) implements Range<U> {
1919
public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, U> attribute, CriteriaBuilder builder) {
2020
return root.get( attribute ).in( values.stream().map( builder::literal ).toList() );
2121
}
22+
23+
@Override @SuppressWarnings("unchecked")
24+
public Class<? extends U> getType() {
25+
return (Class<? extends U>) values.get(0).getClass();
26+
}
2227
}

0 commit comments

Comments
 (0)