Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

java - How use spring boot to create dynamic repository object

I need some help at this error.

I need create a function that use @Autowired into my model client, but when I do popup and I get this error

java.lang.NullPointerException: Cannot invoke "com.teste.cadastro.repository.ClienteRepository.findCPF(String)" because "this.clienteRepository" is null"`

I'm trying to dynamically create this @autowired object without success, if someone of us know how do this, tell me I will appreciate it.

Cliente.java

package com.teste.cadastro.model;

import java.sql.Date;
import java.util.HashMap;
import java.util.InputMismatchException;

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

import org.springframework.beans.factory.annotation.Autowired;

import com.teste.cadastro.repository.ClienteRepository;

@Entity
@Table(name="clientes")
public class Cliente{
    
    @Autowired
    ClienteRepository clienteRepository;    
    
    private long id;

    private String nome;
    private String email;
    private String cpf;
    private Date   dta_nasc;
    
    public Cliente() {

    };
    
    public Cliente(long id, String nome, String email, String cpf) {
        super();
        this.id = id;
        this.nome = nome;
        this.email = email;
        this.cpf = cpf;
    }
    
    public Date getDta_nasc() {
        return dta_nasc;
    }

    public void setDta_nasc(Date dta_nasc) {
        this.dta_nasc = dta_nasc;
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getCpf() {
        return cpf;
    }
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }
    @SuppressWarnings("deprecation")
    public HashMap<String, String> valido() {
        HashMap<String, String> aceito = new HashMap<String, String>();
        if (nome.length()>10)
            aceito.put("nome_valid", "");
        else
            aceito.put("nome_valid", "O nome deve ter mais que dez caracteres");
        
        if (ValidaCPF(cpf))
            if (clienteRepository.findCPF(cpf).size()>0)
                aceito.put("cpf_valid", "");
            else
                aceito.put("cpf_valid", "CPF Já cadastrado");
        else
            aceito.put("cpf_valid", "CPF Informado é invalido");
        
        if (!(dta_nasc == null)) {
        if (dta_nasc.getDate() == 0)
            aceito.put("dta_nasc_valid", "Data informada invalida");
        else
            aceito.put("dta_nasc_valid", "");
        }
        else
            aceito.put("dta_nasc_valid", "Erro na valida??o da data, informe novamente");
        
        if (email.contains("@")) 
            aceito.put("email_valid",  "");
        else
            aceito.put("email_valid",  "Email informado invalido");
        
        if ((aceito.get("nome_valid").length()==0) && (aceito.get("cpf_valid").length()==0) && (aceito.get("dta_nasc_valid").length()==0) && (aceito.get("email_valid").length()==0)) 
            aceito.put("valido", "1");
        else
            aceito.put("valido",  "0");
        
        
        return aceito;
    }
    
    
private boolean ValidaCPF(String CPF) {
        
        CPF = CPF.replace(".", "");
        CPF = CPF.replace("-", "");

        
        if (CPF.equals("00000000000") ||
            CPF.equals("11111111111") ||
            CPF.equals("22222222222") || CPF.equals("33333333333") ||
            CPF.equals("44444444444") || CPF.equals("55555555555") ||
            CPF.equals("66666666666") || CPF.equals("77777777777") ||
            CPF.equals("88888888888") || CPF.equals("99999999999") ||
            (CPF.length() != 11))
            return(false);

        char dig10, dig11;
        int sm, i, r, num, peso;

        try {
            sm = 0;
            peso = 10;
            for (i=0; i<9; i++) {
            num = (int)(CPF.charAt(i) - 48);
            sm = sm + (num * peso);
            peso = peso - 1;
            }

            r = 11 - (sm % 11);
            if ((r == 10) || (r == 11))
                dig10 = '0';
            else dig10 = (char)(r + 48); 

            sm = 0;
            peso = 11;
            for(i=0; i<10; i++) {
            num = (int)(CPF.charAt(i) - 48);
            sm = sm + (num * peso);
            peso = peso - 1;
            }

            r = 11 - (sm % 11);
            if ((r == 10) || (r == 11))
                 dig11 = '0';
            else dig11 = (char)(r + 48);

            if ((dig10 == CPF.charAt(9)) && (dig11 == CPF.charAt(10)))
                 return(true);
            else return(false);
                } catch (InputMismatchException erro) {
                return(false);
            }
        }
}

ClienteController.java

ackage com.teste.cadastro.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.teste.cadastro.model.Cliente;
import com.teste.cadastro.repository.ClienteRepository;

@RequestMapping("/")
@Controller
public class ClientController {
    
    @Autowired
    ClienteRepository clienteRepository;
    
    @CrossOrigin
    @PostMapping(value = "/reg", consumes = "application/json", produces = "application/json")
    public ResponseEntity<HashMap<String, String>> CreateRegistry(@RequestBody Cliente cliente) {
        HashMap<String, String> aceito = cliente.valido();  
        if (aceito.get("valido")=="1") {
            clienteRepository.save(cliente);    
    }
    
    return new ResponseEntity<>(aceito, HttpStatus.OK);
    }
}
package com.teste.cadastro.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.teste.cadastro.model.Cliente;


@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Long>{

    @Query(value= "SELECT u FROM Cliente u WHERE u.cpf = :cpf" , nativeQuery = false)
    List<Cliente> findCPF(String cpf);
    
}
question from:https://stackoverflow.com/questions/65947775/how-use-spring-boot-to-create-dynamic-repository-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...