0

According to this post comment, Hibernate 6 introduced support for the short hand syntax:

In Hibernate 6 we introduced support for the short hand syntax. For Hibernate 5 I believe that you have to use the FQN in HQL...

Unfortunately I cannot find anything on the Internet that explains how to replace a fully qualified name inside a JPA query using short hand syntax.

So the initial situation would be something like this:

@Query("""
        SELECT email
        FROM UserEmail email
        WHERE email.emailStatus.description = com.example.entity.EmailStatus$Description.ACTIVE
        """)
Optional<UserEmail> findAllWithActiveStatus();

And as you might guess right, Description is an enum which lives as an inner class inside EmailStatus. Now the goal would be to get the where-clause down by the use of short hand syntax to something like that:

@Query("""
        SELECT email
        FROM UserEmail email
        WHERE email.emailStatus.description = Description.ACTIVE
        """)
Optional<UserEmail> findAllWithActiveStatus();

Can anyone explain to me what Christian Beikov might have meant by his statement regarding the new Hibernate 6 short hand syntax and how I can use this feature to simplify my code?

Many thanks in advance for any help

Slevin
  • 617
  • 1
  • 7
  • 17

1 Answers1

1

This should work:

SELECT email
FROM UserEmail email
WHERE email.emailStatus.description = ACTIVE

The type of ACTIVE will be inferred from the type of the description attribute.

Gavin King
  • 3,182
  • 1
  • 13
  • 11
  • Thank you for the explanation about inference. Is there a way to get Hibernate recognizing that we deal with an inner class, like in this special case? Unfortunately, it seems that inference allone is not sufficient, since I get the following `SemanticException`: `org.hibernate.query.SemanticException: Could not interpret path expression 'com.example.entity.EmailStatus.Description.ACTIVE'. I think, the proper path expression should be `com.example.entity.EmailStatus$Description.ACTIVE`. – Slevin Apr 30 '23 at 00:13
  • I mean if that is the message, the you have not used the query I just gave you. In the query I just gave you, the expression is `ACTIVE` not `com.example.entity.EmailStatus.Description.ACTIVE`. And yes, if you have an inner class, it should be a `$`, not a `.` i.e. `EmailStatus$Description`. – Gavin King Apr 30 '23 at 12:33
  • Yes, I've understand correctly, I've just used the expression as you meant. To be exactly clear, the where-clause looks as follows: `WHERE email.emailStatus.description = ACTIVE`. But nevertheless, Hibernate then seems to construct via inference an expression by itself with the following fully qualified expression: `com.example.entity.EmailStatus.Description.ACTIVE` (note that this expression build by Hibernate does not have the $-sign separator but inserts just a dot for concatenation). – Slevin Apr 30 '23 at 12:52
  • The result is the SemanticException like mentioned above. So if the beahavior should be as you meant, Hibernate seems to have a bug ;) – Slevin Apr 30 '23 at 12:52
  • 1
    I mean, that's _possible_ of course, but it's pretty difficult to believe, since we have tests for this, and I just tested it myself in 6.3 and it works perfectly, just as advertised. There might be something weird about your mappings, which you have not shown in the problem description. – Gavin King Apr 30 '23 at 13:04
  • 1
    You are right, new releases of Hibernate don't suffer from this bug anymore. I'm using Spring Boot (3.0.6 - the latest stable version) which pulls `hibernate-core-6.1.7.Final`, and this very Hibernate version isn't able to resolve the path correctly. Manually updating Hibernate to `hibernate-core-6.2.2.Final` solves that problem. I'm sorry bothering you, but I was really thinking Spring Boot is getting developed bullet-proof - at least in combination with the most important dependencies like Hibernate or Jackson. My fault, always expect the unexpected. MANY THANKS !!! – Slevin Apr 30 '23 at 13:39