Class TypecheckUtil
x = y
or x > y
. An
assignment is an element of the set
clause in an update
query, or
an element of the values
list in an insert query
.
The rules here are not the same as the rules for the Java language, nor are they identical to the rules for SQL. For example:
- In Java, a comparison expression like
'1'.equals(1)
is well-typed, and evaluates tofalse
. In most SQL dialects, this expression evaluates totrue
, via implicit type conversions. Here we reject such comparison expressions. - In Java, if two classes are related by inheritance, then one is assignable to the other. But here, this assignment is only legal if the two classes are entity types belonging to the same mapped entity inheritance hierarchy.
- On the other hand, in Java,
java.sql.Date
andjava.time.LocalDate
may not be compared nor assigned. But here they're considered inter-comparable and inter-assignable.
Two basic types are considered comparable if they map to the same "family" of JDBC types. Here we allow some latitude so that different numeric types are comparable, and different string types are comparable. However, we do not allow comparisons between types which involve more questionable/unportable implicit type conversions (between integers and strings, for example). This means that we accept comparisons between basic types which map completely unrelated types in Java.
Entity types have identity equality. That is, two entities are considered equal if their primary keys are equal.
Embeddable and tuple types have value equality. That is, they're considered equal if their members are equal. For convenience, an embeddable object may be compared directly to a tuple constructor.
Comparison of discriminators (that is, of literal entity types and type()
function application) is legal only when the entity types belong to the same mapped
entity hierarchy.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areTypesComparable
(SqmExpressible<?> lhsType, SqmExpressible<?> rhsType, BindingContext bindingContext) static void
assertAssignable
(String hqlString, SqmPath<?> targetPath, SqmTypedNode<?> expression, BindingContext bindingContext) static void
assertComparable
(Expression<?> x, Expression<?> y, BindingContext bindingContext) static void
assertDuration
(SqmExpression<?> expression) static void
assertNumeric
(SqmExpression<?> expression, UnaryArithmeticOperator op) static void
assertOperable
(SqmExpression<?> left, SqmExpression<?> right, BinaryArithmeticOperator op) static void
assertString
(SqmExpression<?> expression) static boolean
isNumberArray
(SqmExpressible<?> expressible)
-
Constructor Details
-
TypecheckUtil
public TypecheckUtil()
-
-
Method Details
-
areTypesComparable
public static boolean areTypesComparable(SqmExpressible<?> lhsType, SqmExpressible<?> rhsType, BindingContext bindingContext) - Parameters:
lhsType
- the type of the expression on the LHS of the comparison operatorrhsType
- the type of the expression on the RHS of the comparison operator- See Also:
-
isTypeAssignable(SqmPathSource, SqmExpressible, BindingContext)
- Implementation Note:
- The code below will reject some things that are accepted in H5, and perhaps even a few things which were accepted in H6.1/6.2. Therefore, users will report "bugs". The correct resolution of such bugs is not to go naively inserting special cases and secret escape hatches in this code! Instead, it's important to practice saying "no" and rejecting the majority of such "bug" reports. In cases where a fix really is required, it should be done within the framework laid out below, not by adding ad-hoc special rules and holes which undermine the type system. It's much more important that HQL has simple, predictable, and understandable rules than it is that every single user be able to do every single little weird thing that used to work in Hibernate 5.
-
assertComparable
public static void assertComparable(Expression<?> x, Expression<?> y, BindingContext bindingContext) -
assertAssignable
public static void assertAssignable(String hqlString, SqmPath<?> targetPath, SqmTypedNode<?> expression, BindingContext bindingContext) -
assertOperable
public static void assertOperable(SqmExpression<?> left, SqmExpression<?> right, BinaryArithmeticOperator op) -
isNumberArray
-
assertString
-
assertDuration
-
assertNumeric
-