If you want to stop extending the JpaRepository
interface to every class in your Java project, you can use an abstract class or an interface to define common methods and annotations that can be inherited by your repository classes.
Here's an example of how you can use an abstract class to define common methods and annotations:
@NoRepositoryBean public abstract class BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { // Common methods and annotations go here } public interface UserRepository extends BaseRepository<User, Long> { // Custom methods for the User entity go here }
This code defines an abstract class called BaseRepository
that extends the JpaRepository
interface and has the @NoRepositoryBean
annotation. The @NoRepositoryBean
annotation tells Spring not to create a bean for the BaseRepository
class, so it can be used as a base class for other repository interfaces.
The UserRepository
interface then extends the BaseRepository
class and adds custom methods for the User
entity. This allows you to define common methods and annotations in the BaseRepository
class and reuse them in your repository interfaces without having to repeat the same code.
Alternatively, you can use an interface to define common methods and annotations, and use the @NoRepositoryBean
annotation on the interface to tell Spring not to create a bean for it.
Here's an example of how you can do this:
@NoRepositoryBean public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { // Common methods and annotations go here } public interface UserRepository extends BaseRepository<User, Long> { }