Class TypecheckUtil
- java.lang.Object
-
- org.hibernate.query.sqm.internal.TypecheckUtil
-
public class TypecheckUtil extends Object
Functions for typechecking comparison expressions and assignments in the SQM tree. A comparison expression is any predicate likex = y
orx > y
. An assignment is an element of theset
clause in anupdate
query, or an element of thevalues
list in aninsert 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. - In Java, a comparison expression like
-
-
Constructor Summary
Constructors Constructor Description TypecheckUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areTypesComparable(SqmExpressible<?> lhsType, SqmExpressible<?> rhsType, SessionFactoryImplementor factory)
static void
assertAssignable(String hqlString, SqmPath<?> targetPath, SqmTypedNode<?> expression, SessionFactoryImplementor factory)
static void
assertComparable(Expression<?> x, Expression<?> y, SessionFactoryImplementor factory)
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)
-
-
-
Method Detail
-
areTypesComparable
public static boolean areTypesComparable(SqmExpressible<?> lhsType, SqmExpressible<?> rhsType, SessionFactoryImplementor factory)
- 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, SessionFactoryImplementor)
- 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, SessionFactoryImplementor factory)
-
assertAssignable
public static void assertAssignable(String hqlString, SqmPath<?> targetPath, SqmTypedNode<?> expression, SessionFactoryImplementor factory)
-
assertOperable
public static void assertOperable(SqmExpression<?> left, SqmExpression<?> right, BinaryArithmeticOperator op)
-
assertString
public static void assertString(SqmExpression<?> expression)
-
assertDuration
public static void assertDuration(SqmExpression<?> expression)
-
assertNumeric
public static void assertNumeric(SqmExpression<?> expression, UnaryArithmeticOperator op)
-
-