How to solve the PostgreSQL :: cast operator issue with JPA and Hibernate

Introduction While answering questions on the Hibernate forum, I stumbled on the following question regarding the PostgreSQL :: cast operation used in a JPA and Hibernate entity query. Since it’s a very interesting use case, I decided to turn the answer into a dedicated blog post.