Skip to content

Commit 47eb5a2

Browse files
authored
Add documentation about scope features (#663)
1 parent 78c49d5 commit 47eb5a2

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

docs/criteria-api.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,77 @@ The above query issues the following SQL statement:
15941594
)
15951595
where t0_.EMPLOYEE_ID = ?
15961596
1597+
Scopes (Entityql, NativeSql)
1598+
==========================================
1599+
1600+
Scoping allow you to specify commonly-used query conditions.
1601+
1602+
To define a simple scope,
1603+
create the class which has a method annotated with ``@Scope``:
1604+
1605+
.. code-block:: java
1606+
1607+
public class DepartmentScope {
1608+
@Scope
1609+
public Consumer<WhereDeclaration> onlyTokyo(Department_ d) {
1610+
return c -> c.eq(d.location, "Tokyo");
1611+
}
1612+
}
1613+
1614+
To enable the scope,
1615+
specify the above class in the scopes element of ``@Metamodel``:
1616+
1617+
.. code-block:: java
1618+
1619+
@Entity(metamodel = @Metamodel(scopes = { DepartmentScope.class }))
1620+
public class Department { ... }
1621+
1622+
Now the metamodel ``Department_`` has a ``onlyTokyo`` method.
1623+
You can use it as follows:
1624+
1625+
.. code-block:: java
1626+
1627+
Department_ d = new Department_();
1628+
1629+
List<Department> list = entityql.from(d).where(d.onlyTokyo()).fetch();
1630+
1631+
The above query issues the following SQL statement:
1632+
1633+
.. code-block:: sql
1634+
1635+
select t0_.DEPARTMENT_ID, t0_.DEPARTMENT_NO, t0_.DEPARTMENT_NAME, t0_.LOCATION, t0_.VERSION from DEPARTMENT t0_
1636+
where t0_.LOCATION = ?
1637+
1638+
When you want to combine other query conditions with scopes,
1639+
compose them using the `andThen` method:
1640+
1641+
.. code-block:: java
1642+
1643+
Department_ d = new Department_();
1644+
1645+
List<Department> list = entityql.from(d).where(d.onlyTokyo().andThen(c -> c.gt(d.departmentNo, 50))).fetch();
1646+
1647+
You can define several scopes in a class as follows:
1648+
1649+
.. code-block:: java
1650+
1651+
public class DepartmentScope {
1652+
@Scope
1653+
public Consumer<WhereDeclaration> onlyTokyo(Department_ d) {
1654+
return c -> c.eq(d.location, "Tokyo");
1655+
}
1656+
1657+
@Scope
1658+
public Consumer<WhereDeclaration> locationStartsWith(Department_ d, String prefix) {
1659+
return c -> c.like(d.location, prefix, LikeOption.prefix());
1660+
}
1661+
1662+
@Scope
1663+
public Consumer<OrderByNameDeclaration> sortByNo(Department_ d) {
1664+
return c -> c.asc(d.departmentNo);
1665+
}
1666+
}
1667+
15971668
Tips
15981669
====
15991670

0 commit comments

Comments
 (0)