Hibernate has no @SqlFunction annotation. The class and annotation shown in many articles, including the earlier version of this answer, do not exist in Hibernate 5 or 6, and code using them does not compile. To use a custom SQL function in HQL you register it through a FunctionContributor (Hibernate 6) or a MetadataBuilderContributor (Hibernate 5), or you call a database function directly with the HQL function() call.
This article shows the supported ways to expose your own SQL functions to HQL and Criteria queries.
Hibernate keeps SQL functions in a registry that is built when the SessionFactory starts. Functions are registered programmatically, by dialect or by contributors, not by marking a Java method. A static Java method cannot be translated to SQL anyway, because the function body runs inside the database. So there is no annotation that maps a Java method to a SQL function.
The real options are:
| Need | Approach |
|---|---|
| Call a function once, with no setup | function('name', args) in HQL |
| Reusable function with its own name and return type | FunctionContributor (Hibernate 6) |
| Same in Hibernate 5 | MetadataBuilderContributor with SQLFunctionTemplate |
| Computed value on an entity | @Formula |
In Hibernate 6 and in JPA 2.1 and later, HQL can call any function by name. Hibernate passes the call to the database unchanged if it does not know the name:
This produces SQL like where reverse(e.name) = ?. It is the quickest option and works for functions that exist in your database, such as reverse in PostgreSQL or MySQL. The drawback is that the return type is not declared, which can affect comparisons and result mapping.
In Hibernate 6, implement org.hibernate.boot.model.FunctionContributor and register a SQL pattern with a name, argument count and return type:
Hibernate discovers contributors through the Java service loader. Create the file META-INF/services/org.hibernate.boot.model.FunctionContributor containing the fully qualified class name:
After that, HQL can use the function like a built-in one:
The pattern reverse(?1) is the SQL template, and ?1 is the first argument. Check the exact builder methods against the Hibernate version you use, since the function registry API has changed between 6.x releases.
Hibernate 5 registers functions with a MetadataBuilderContributor and sets the hibernate.metadata_builder_contributor property:
Hibernate 5 is no longer the current line, so use this only for legacy applications and plan an upgrade.
When you only need a derived value on an entity, @Formula is simpler than a function:
Hibernate adds the SQL fragment to the select list, and the field is read-only.
Keep functions portable where you can. Patterns that call vendor-specific functions tie your queries to one database, so test against the real engine, for example with Testcontainers, instead of an embedded database. Prefer standard HQL functions first, as Hibernate 6 already supports many, such as length, concat, substring and coalesce.
No. Hibernate registers functions through contributors, dialects or the HQL function() call.
Use function('name', arguments) for a one-off call, or register it with a FunctionContributor to give it a typed, reusable name.
Yes. Use CriteriaBuilder.function("name", Type.class, expression) for any function name.
Yes. Register it in a contributor, and only load that contributor in the deployment that targets the database.
Hibernate has no @SqlFunction. Use function() for quick calls, a FunctionContributor in Hibernate 6 or a MetadataBuilderContributor in Hibernate 5 for reusable named functions, and @Formula for computed entity fields. Verify the registry API for your Hibernate version and test on the real database.