Skip to content

Add example for Spring Data RepositoryDefinition#204

Closed
rashidi wants to merge 1 commit intomasterfrom
data/annotation
Closed

Add example for Spring Data RepositoryDefinition#204
rashidi wants to merge 1 commit intomasterfrom
data/annotation

Conversation

@rashidi
Copy link
Copy Markdown
Owner

@rashidi rashidi commented Mar 21, 2025

No description provided.

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.
@rashidi rashidi added the enhancement New feature or request label Mar 21, 2025
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @RepositoryDefinition Example: A new module data-repository-definition is created to showcase the usage of @RepositoryDefinition for defining custom Spring Data repositories.
  • Custom Repository Interface: The NoteRepository interface is defined using @RepositoryDefinition, specifying the domain class (Note) and ID class (Long), and includes a custom method findByTitleContainingIgnoreCase.
  • Testcontainers Integration: The example includes a test case using Testcontainers and PostgreSQL to verify the functionality of the custom repository, ensuring that the findByTitleContainingIgnoreCase method works as expected.
  • Removal of JpaRepository Extension: The BookAvailabilityRepository and BookRepository interfaces in the data-domain-events module are modified to use @RepositoryDefinition instead of extending JpaRepository, 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 JpaRepository extension with @RepositoryDefinition annotation.
    • Added save method to the repository interface.
  • data-domain-events/src/main/java/zin/rashidi/boot/data/de/book/BookRepository.java
    • Replaced JpaRepository extension with @RepositoryDefinition annotation.
    • Added findById, save, and delete methods to the repository interface.
  • 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 @RepositoryDefinition with a sample code and test case.
  • 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 Note record representing a simple domain object with id, title, and content fields.
  • data-repository-definition/src/main/java/zin/rashidi/data/repositorydefinition/note/NoteRepository.java
    • Created a NoteRepository interface using @RepositoryDefinition with a custom findByTitleContainingIgnoreCase method.
  • 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 NoteRepository using Spring Boot's testing support and Testcontainers.
  • settings.gradle
    • Included the data-repository-definition module in the overall project.
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

  1. 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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +10 to +11
@RepositoryDefinition(domainClass = BookAvailability.class, idClass = Long.class)
interface BookAvailabilityRepository {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)

Comment on lines +10 to +11
@RepositoryDefinition(domainClass = Book.class, idClass = Long.class)
public interface BookRepository {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)

@sonarqubecloud
Copy link
Copy Markdown

@rashidi rashidi closed this Mar 21, 2025
@rashidi rashidi deleted the data/annotation branch April 4, 2025 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant