JPA specification says:
Only the values of like types are permitted to be compared. A type is like another type if they correspond to the same Java language type, or if one is a primitive Java language type and the other is the
wrappered Java class type equivalent (e.g., int and Integer are like types in this sense). There is one
exception to this rule: it is valid to compare numeric values for which the rules of numeric promotion
apply. Conditional expressions attempting to compare non-like type values are disallowed except for
this numeric case.Note that the arithmetic operators and comparison operators are permitted to be applied to
state fields and input parameters of the wrappered Java class equivalents to the primitive
numeric Java types.Two entities of the same abstract schema type are equal if and only if they have the same primary key
value.Only equality/inequality comparisons over enums are required to be supported.
Comparisons over instances of embeddable class or map entry types are not supported.
Does this mean that queries
where entity1 = entity2
and
where entity1.id = entity2.id
should always give the same result (and have same performance) or are there some edge cases (e.g. if one of entities is null)? Is the behavior the same in all implementations?
These can be different, depending where your entity comes from, and your JPA implementation.
entity1 = entity2, may allow the JPA implementation to optimize out a join in certain cases.