Hibernate – How do I work in the JPQL WHERE clause?

How to evaluate entity comparison (equal) statements in JPQL: Is it through identity comparison, equals() or something else?

I spent a few hours Googling and completed the Hibernate and JPA specifications, but still can’t find how it works. Consider the following entities:

< /p>

class MyProductType{Integer id;}
class MyProduct{Integer id; MyProductType pType;}

And the current JPQL / HQL query:

< /p>

SELECT t FROM MyProductType t, MyProduct p WHERE p.pType = t

(I know this is an ugly query, I only pay attention to where clause semantics.)

< p>So how to evaluate p.pType = t?

JSR 317 mentions the “entity_expression” comparison, but its behavior is not clarified.

Edit: I don’t like the following Rika’s suggestion that the .id method includes implicit inner joins , If the query uses OUTER (LEFT) connection, it is usually not what you want.

I’m on http ://www.objectdb.com/java/jpa/query/jpql/comparison found this, very interesting, very good question.

Instances of user defined classes (entity classes and embeddable
classes) can be compared by using the equality operators (=, <>, ==,
!=). For entities, e1 = e2 if e1 and e2 have the same type and the
same primary key value. For embeddable objects, e1 = e2 if e1 and e2
have exactly the same content.

So it seems to check the object’s The primary key value and the type of the object. So it seems that p.pType = t, it will check (assuming id is the primary key) the id of p.pType, id is t and see if they are equal. Then it will check if the two entities belong Same type or MyProductType.

How to evaluate entity comparison (equal) statements in JPQL: Is it through identity comparison, equals() or other?

I spent a few hours Googling and completed the Hibernate and JPA specifications, but still can’t find how it works. Consider the following entities:

< /p>

class MyProductType{Integer id;}
class MyProduct{Integer id; MyProductType pType;}

And the current JPQL / HQL query:

< /p>

SELECT t FROM MyProductType t, MyProduct p WHERE p.pType = t

(I know this is an ugly query, I only pay attention to where clause semantics.)

< p>So how to evaluate p.pType = t?

JSR 317 mentions the “entity_expression” comparison, but its behavior is not clarified.

Edit: I don’t like the following Rika’s suggestion that the .id method includes implicit inner joins , If the query uses OUTER (LEFT) connection, it is usually not what you want.

I am at http://www.objectdb.com/java/jpa /query/jpql/comparison found this, very interesting, very good question.

Instances of user defined classes (entity classes and embeddable
classes) can be compared by using the equality operators (=, <>, ==,
!=). For entities, e1 = e2 if e1 and e2 have the same type and the
same primary key value . For embeddable objects, e1 = e2 if e1 and e2
have exactly the same content.

So it seems to check the primary key value of the object and the type of the object. Therefore, it seems that p .pType = t, it will check (assuming id is the primary key) the id of p.pType, id is t and see if they are equal. Then it will check if the two entities are of the same type or MyProductType.

Leave a Comment

Your email address will not be published.