Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit a7663c5

Browse files
authored
Add the description about the user-defined expressions (#16)
1 parent f21f4f5 commit a7663c5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/criteria-api.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,43 @@ The above query issues the following SQL statement:
17971797
)
17981798
where t0_.EMPLOYEE_ID = ?
17991799
1800+
User-defined expressions
1801+
------------------------
1802+
1803+
You can define user-defined expressions by calling ``Expressions.userDefined``.
1804+
1805+
In the example below, the replace function is defined:
1806+
1807+
.. code-block:: java
1808+
1809+
UserDefinedExpression<String> replace(PropertyMetamodel<String> expression, PropertyMetamodel<String> from, PropertyMetamodel<String> to) {
1810+
return Expressions.userDefined(expression, "replace", from, to, c -> {
1811+
c.appendSql("replace(");
1812+
c.appendExpression(expression);
1813+
c.appendSql(", ");
1814+
c.appendExpression(from);
1815+
c.appendSql(", ");
1816+
c.appendExpression(to);
1817+
c.appendSql(")");
1818+
});
1819+
}
1820+
1821+
You can use the replace function in your query as follows:
1822+
1823+
.. code-block:: java
1824+
1825+
Department_ d = new Department_();
1826+
1827+
List<String> list =
1828+
nativeSql
1829+
.from(d).select(replace(d.location, Expressions.literal("NEW"), Expressions.literal("new"))).fetch();
1830+
1831+
The above query issues the following SQL statement:
1832+
1833+
.. code-block:: sql
1834+
1835+
select replace(t0_.LOCATION, 'NEW', 'new') from DEPARTMENT t0_
1836+
18001837
Scopes (Entityql, NativeSql)
18011838
==========================================
18021839

0 commit comments

Comments
 (0)