Add example for Spring Data RepositoryDefinition#204
Conversation
Introduce a new `data-repository-definition` module demonstrating the use of `@RepositoryDefinition`. Refactor existing repositories to use the annotation for cleaner and more focused APIs.
There was a problem hiding this comment.
Hello @rashidi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request introduces an example demonstrating the use of Spring Data's @RepositoryDefinition annotation. This annotation allows developers to define custom repository interfaces without extending standard Spring Data base interfaces like JpaRepository. The changes include creating a new module data-repository-definition with a sample Note entity and a corresponding NoteRepository interface defined using @RepositoryDefinition. The example also includes a test case using Testcontainers to verify the functionality of the custom repository.
Highlights
- Introduction of
@RepositoryDefinitionExample: A new moduledata-repository-definitionis created to showcase the usage of@RepositoryDefinitionfor defining custom Spring Data repositories. - Custom Repository Interface: The
NoteRepositoryinterface is defined using@RepositoryDefinition, specifying the domain class (Note) and ID class (Long), and includes a custom methodfindByTitleContainingIgnoreCase. - Testcontainers Integration: The example includes a test case using Testcontainers and PostgreSQL to verify the functionality of the custom repository, ensuring that the
findByTitleContainingIgnoreCasemethod works as expected. - Removal of JpaRepository Extension: The
BookAvailabilityRepositoryandBookRepositoryinterfaces in thedata-domain-eventsmodule are modified to use@RepositoryDefinitioninstead of extendingJpaRepository, demonstrating an alternative approach to repository definition.
Changelog
Click here to see the changelog
- data-domain-events/src/main/java/zin/rashidi/boot/data/de/availability/BookAvailabilityRepository.java
- Replaced
JpaRepositoryextension with@RepositoryDefinitionannotation. - Added
savemethod to the repository interface.
- Replaced
- data-domain-events/src/main/java/zin/rashidi/boot/data/de/book/BookRepository.java
- Replaced
JpaRepositoryextension with@RepositoryDefinitionannotation. - Added
findById,save, anddeletemethods to the repository interface.
- Replaced
- data-repository-definition/.gitattributes
- Added file to define attributes for files like gradlew, bat, and jar.
- data-repository-definition/.gitignore
- Added file to exclude files and directories from being tracked by Git, such as build outputs, IDE configuration files, and temporary files.
- data-repository-definition/README.adoc
- Created a README file explaining the purpose and usage of
@RepositoryDefinitionwith a sample code and test case.
- Created a README file explaining the purpose and usage of
- data-repository-definition/build.gradle
- Configured Gradle build file with dependencies for Spring Boot, Data JDBC, PostgreSQL, and Testcontainers.
- data-repository-definition/settings.gradle
- Defined the root project name.
- data-repository-definition/src/main/java/zin/rashidi/data/repositorydefinition/DataRepositoryDefinitionApplication.java
- Created a Spring Boot application class.
- data-repository-definition/src/main/java/zin/rashidi/data/repositorydefinition/note/Note.java
- Created a
Noterecord representing a simple domain object withid,title, andcontentfields.
- Created a
- data-repository-definition/src/main/java/zin/rashidi/data/repositorydefinition/note/NoteRepository.java
- Created a
NoteRepositoryinterface using@RepositoryDefinitionwith a customfindByTitleContainingIgnoreCasemethod.
- Created a
- data-repository-definition/src/main/resources/application.properties
- Defined the application name.
- data-repository-definition/src/test/java/zin/rashidi/data/repositorydefinition/TestDataRepositoryDefinitionApplication.java
- Created a test application class that includes Testcontainers configuration.
- data-repository-definition/src/test/java/zin/rashidi/data/repositorydefinition/TestcontainersConfiguration.java
- Configured Testcontainers to use PostgreSQL for integration tests.
- data-repository-definition/src/test/java/zin/rashidi/data/repositorydefinition/note/NoteRepositoryTests.java
- Created a test class for
NoteRepositoryusing Spring Boot's testing support and Testcontainers.
- Created a test class for
- settings.gradle
- Included the
data-repository-definitionmodule in the overall project.
- Included the
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Trivia time!
What is the primary difference between @Repository and @RepositoryDefinition in Spring Data?
Click here for the answer
`@Repository` is used to mark a class as a data access component, providing exception translation. `@RepositoryDefinition` is used to define a repository interface without extending Spring Data's base repository interfaces.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
The pull request introduces the use of @RepositoryDefinition for Spring Data repositories, providing an example of how to define repositories with specific methods without extending base interfaces. The changes include updates to repository interfaces and the addition of a new module with a sample application and tests. Overall, the changes are well-structured and demonstrate the intended functionality.
Merge Readiness
The code changes introduce a valuable example of using @RepositoryDefinition in Spring Data. I am unable to directly approve the pull request, and recommend that others review and approve this code before merging. There are no critical or high severity issues, so the pull request is ready to be merged once the medium severity issues are addressed.
| @RepositoryDefinition(domainClass = BookAvailability.class, idClass = Long.class) | ||
| interface BookAvailabilityRepository { |
There was a problem hiding this comment.
Consider adding a JavaDoc to explain why we are using @RepositoryDefinition instead of extending JpaRepository. This will help future developers understand the intent and benefits of this approach.
/**
* This repository uses @RepositoryDefinition to define a custom interface
* with only the required methods, instead of extending JpaRepository.
*/
@RepositoryDefinition(domainClass = BookAvailability.class, idClass = Long.class)| @RepositoryDefinition(domainClass = Book.class, idClass = Long.class) | ||
| public interface BookRepository { |
There was a problem hiding this comment.
Similar to the BookAvailabilityRepository, adding a JavaDoc here would be beneficial to explain the rationale behind using @RepositoryDefinition.
/**
* This repository uses @RepositoryDefinition to define a custom interface
* with only the required methods, instead of extending JpaRepository.
*/
@RepositoryDefinition(domainClass = Book.class, idClass = Long.class)
|



No description provided.