Search code examples
hibernatejpahql

How to use Hibernate 6 short hand syntax to simplify JPA query?


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


Solution

  • 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.