Quantcast
Channel: None – Dec's Dom Blog
Viewing all articles
Browse latest Browse all 30

Extending Your Entity Repositories

$
0
0

Now that the basic CRUD interface is working it would be nice if we could extend the interface to make it even more useful. What if we wanted to return a list of all the locations in a particular state or if we wanted to find all the people who are in a particular location?

These sound like queries that you would normally write in SQL with something like ‘SELECT * FROM Locations WHERE State =”PA” ‘. To do this in Spring Boot and Spring Data Repositories you can use the simple query builder to add a single line to your repository.

This will return a List of Location objects when I pass in a string that matches a state. The @Param(“state”) is used as part of the REST interface that I’m also building and allows me to specify how this particular parameter will be passed on the URL.

On my PersonRepository  I have added two additional query builders.

So now I can get a list of Person objects when I pass in a location id or I can get a list of Person objects when I pass in a manager id.

The Query Builder system in Spring Data Repositories is very powerful. findBy is just one of many method names that you can use. You can add additional query keywords like AND, NOT, OR etc. If your data entity has a date field you can do something like findByDateFieldBetween(date1,date2) and the query builder will build a query to find records just between those two dates.

I highly recommend reading the Query Builder documentation.


Viewing all articles
Browse latest Browse all 30

Trending Articles