@NoRepositoryBean

The @NoRepositoryBean annotation is another way to fine-tune the spring data JPA repository. @NoRepositoryBean annotation concept is just like Interface in Java.

The interface has the declaration of all methods, and a base class implements the interface and extends all the methods. The interface is not allowed to create objects in the application.

Just like an Interface, a base Repository will be created and annotated with @NoRepositoryBean annotation. The baseRepository has all the required methods declared.

A custom user-defined Repository extends the base Repository. The base Repository with @NoRepositoryBean annotation is not allowed to create a @Bean in the application context, custom user-defined Repository can create @Bean and will be used in the application.

drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
CREATE TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
CREATE TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;
insert into hibernate_sequence values ( 6 );
insert into apple (apple_id, apple_name ) values (1, "Macintosh");
insert into apple (apple_id, apple_name ) values (2, "Fuji");
insert into apple (apple_id, apple_name ) values (3, "Gala");
insert into apple (apple_id, apple_name ) values (4, "Jonagold");
insert into apple (apple_id, apple_name ) values (5, "GrannySmith");
package com.sujan.example.jpa.controller;

import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AppleController {
    @Autowired
    private AppleRepository appleRepository;

    @GetMapping("save")
    Apple save() {
        Apple apple = new Apple("PinkLady");
        Apple save = appleRepository.save(apple);
        System.out.println("save = " + save);
        return save;
    }

    @GetMapping("findById")
    Apple findById() {
        Apple findById = appleRepository.findById(1L).orElseThrow();
        System.out.println("findById = " + findById);
        return findById;
    }

    @GetMapping("findAll")
    List<Apple> findAll() {
        List<Apple> all = appleRepository.findAll();
        System.out.println("all = " + all);
        return all;
    }

    @GetMapping("deleteById")
    void deleteById() {
        appleRepository.deleteById(1L);
    }
}
package com.sujan.example.jpa.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity
@NoArgsConstructor
public class Apple {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long appleId;
    private String appleName;

    public Apple(String appleName) {
        this.appleName = appleName;
    }
}
package com.sujan.example.jpa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.List;
import java.util.Optional;

@NoRepositoryBean
public interface AppleBaseRepository<T, ID> extends JpaRepository<T, ID> {
    <S extends T> S save(S entity);

    Optional<T> findById(ID id);

    List<T> findAll();

    void deleteById(ID id);
}
package com.sujan.example.jpa.repository;

import com.sujan.example.jpa.entity.Apple;

public interface AppleRepository extends AppleBaseRepository<Apple, Long> {
}
package com.sujan.example.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
    id 'org.springframework.boot' version '2.3.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/save'
curl --location --request GET 'http://localhost:8080/findById'
curl --location --request GET 'http://localhost:8080/findAll'
curl --location --request GET 'http://localhost:8080/deleteById'

 

follow us on