A dynamic finder method is a finder method for which you don't need to write a JPA query. It fetches entity instances from the database based on one or more persistent fields of the entity class. The implementation of these dynamic finder methods is auto-generated by Roo when you add their names to a persistent entity. As Roo doesn't create a DAO layer of an application, dynamic finder methods are defined in the entity class. In this recipe, we will look at the finder list command, which introspects a persistent entity and suggests names of possible dynamic finder methods that can be added to the given persistent entity.
Create a sub-directory ch03-recipes
inside the C:
oo-cookbook
directory.
Execute the ch03_persistent_entities.roo
script to create a flight-app
Roo project. The script sets up Hibernate as a persistence provider and creates a Flight
entity, which has FlightKey
as its composite primary key class. Additionally, the script adds fields to the Flight
and FlightKey
classes. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.
Start Roo shell from the C:
oo-cookbookch03-recipes
directory.
To use the finder
list
command to view dynamic finder methods, follow the given steps:
Flight
entity using the focus
command:roo> focus --class ~.domain.Flight
finder
list
command to view the list of candidate dynamic finder methods for the Flight
entity, as shown here:~.domain.Flight roo> finder list ..... findFlightsByCreatedDateBetween(Date minCreatedDate, Date maxCreatedDate) findFlightsByCreatedDateGreaterThan(Date createdDate) ..... findFlightsByDestination(String destination) findFlightsByDestinationEquals(String destination) findFlightsByDestinationIsNotNull() findFlightsByDestinationIsNull() findFlightsByDestinationLike(String destination) findFlightsByDestinationNotEquals(String destination)
The finder
list
command displays names of the candidate dynamic finder methods for an entity. By default, the dynamic finder methods suggested by the finder
list
command search for entity instances based on only one persistent field of the entity. For instance, in the output of the finder
list
command you will not find a dynamic finder method name which finds the Flight
entity instances based on both the createdDate
and modifiedDate
fields. If an entity inherits from a mapped superclass, then the dynamic finder methods corresponding to the inherited fields are also displayed by Roo.
The dynamic finder methods suggested by the finder
list
command are dependent upon the type of the field. For instance, the createdDate
is of type java.util.Date
and can participate in greater than, less than, and between comparisons; therefore, findFlightsByCreatedDateBetween
, findFlightsByCreatedDateGreaterThan,
and so on, are shown as candidate dynamic finder methods, and these methods accept the createdDate
field as the argument. On the other hand, the destination
field is of type String
, which doesn't participate in greater than, less than, and between comparisons; therefore, Roo doesn't suggest dynamic finder methods for finding the Flight
instances based on greater than, less than, and between comparisons of destination
field.
Let's now look at how to instruct Roo to:
If you want Roo to list finder method names that fetch entities based on multiple persistent fields, you should use the depth
argument of the finder
list
command. The depth
argument accepts a numeric value, which determines the number of persistent fields the dynamic finder method uses to search for entities in the database. For instance, if you want Flight
instances to be searched based on both the origin
and destination
fields, then the value of the depth
argument must be 2
. The default value of the depth
argument is 1
, therefore; when we executed the finder
list
command, without specifying depth
argument, it listed dynamic finder method names that fetch entities based on only one persistent field. The following finder
list
command shows the affect of the depth
argument on the suggested list of dynamic finder method names:
~.domain.Flight roo> finder list --depth 2 findFlightsByDestinationAndOrigin(String destination, String origin) findFlightsByDestinationAndOriginEquals(String destination, String origin) .....
As the output suggests, two persistent fields now form part of listed candidate dynamic finder method names. Similarly, if you want your finders to span a n
number of persistent fields, then specify n
as the value of a depth
argument.
As you increase the value of the depth
argument, the number of candidate dynamic finder method names listed by Roo increases exponentially due to the number of possible combinations for method arguments. In such cases, it is desired to filter candidate dynamic finder method names based on a filter criteria. The finder
list
command provides a filter
argument, which accepts a comma separated list of strings that must be present in the dynamic finder method name. Dynamic finder method names which don't contain the strings specified by the filter
argument are omitted from the displayed list of candidate dynamic finder method names. This makes it easy for you to locate dynamic finder methods that you want to add to a persistent entity.
Note that there should be no spaces in the comma-separated list of strings specified as the value of the filter
argument.
The following finder
list
command shows the filter
argument usage:
~.domain.Flight roo> finder list --depth 2 --filter destinationlike,originlike findFlightsByDestinationLikeAndOriginLike(..) findFlightsByDestinationLikeOrOriginLike(..) findFlightsByOriginLikeAndDestinationLike(..) findFlightsByOriginLikeOrDestinationLike(..)
The output now shows only four methods that contain the text specified in the filter
argument. Similarly, you can add additional text to the filter
argument to narrow down the list of candidate dynamic finder method names that are displayed by the finder
list
command.
3.139.70.101