ERROR: null value in column "id_municipio_id_municipio" violates the non-null restriction

Asked

Viewed 368 times

4

This error is occurring when I try to persist the screen data to the database. I checked, by debugging the screen, that the data is being captured. When the method exits line 33 of the image it goes straight to line 45.

inserir a descrição da imagem aqui

the server log is like this:

org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "id_municipio_id_municipio" viola a restrição não-nula
  Detalhe: Registro que falhou contém (22, 5462, null, yurhjfdn, 1, null, null).

Distritocontroller class

@RestController
@RequestMapping(value="/user")
public class DistritosController {

    @Autowired
    DistritosService distritosService; 


    @RequestMapping(method = RequestMethod.POST, value = "/distritos", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Distritos> cadastrarDistritos(@RequestBody Distritos distritos) {
        Distritos distritosCadastrado = distritosService.cadastrar(distritos);
        return new ResponseEntity<Distritos>(distritosCadastrado, HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/distritos", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Distritos>> buscarTodosDistritos() {
        Collection<Distritos> distritosBuscados = distritosService.buscarTodos();
        return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/distritos/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Distritos> buscarDistritosPorId(@PathVariable int id) {
        Distritos distritos = distritosService.buscaPorId(id);
        return new ResponseEntity<>(distritos, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/distritos/{id}")
    public ResponseEntity<Distritos> excluirDistritos(@PathVariable int id) {
        Distritos distritoEncontrado = distritosService.buscaPorId(id);
        if (distritoEncontrado == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        distritosService.excluir(distritoEncontrado);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/distritos", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Distritos> alterarDistritos(@RequestBody Distritos distritos) {
        Distritos distritoAlterado = distritosService.alterar(distritos);
        return new ResponseEntity<Distritos>(distritoAlterado, HttpStatus.OK);
    }


}

Class Districts

@Entity
@Table(name = "distritos", schema="glb")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Distritos.findAll", query = "SELECT d FROM Distritos d"),
    @NamedQuery(name = "Distritos.findByIdDistrito", query = "SELECT d FROM Distritos d WHERE d.idDistrito = :idDistrito"),
    @NamedQuery(name = "Distritos.findByNome", query = "SELECT d FROM Distritos d WHERE d.nome = :nome"),
    @NamedQuery(name = "Distritos.findByCodigoDne", query = "SELECT d FROM Distritos d WHERE d.codigoDne = :codigoDne"),
    @NamedQuery(name = "Distritos.findByFlagAtivo", query = "SELECT d FROM Distritos d WHERE d.flagAtivo = :flagAtivo")})




@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idDistrito", scope= Distritos.class)        
public class Distritos implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_distrito")
    private Long idDistrito;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 70)
    @Column(name = "nome")
    private String nome;
    @Size(max = 8)
    @Column(name = "codigo_dne")
    private String codigoDne;
    @Column(name = "flag_ativo")
    private Integer flagAtivo;
    @JoinColumn()
    @ManyToOne
    private Entidades idEntidade;
    @JoinColumn()
    @ManyToOne(optional = false)
    private Municipios idMunicipio;
    @JoinColumn()
    @ManyToOne(optional = false)
    private Ufs idUf;

    public Distritos() {
    }

   gets and sets . ..

}

Classe Municipios

@Entity
@Table(name = "municipios", schema="glb")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Municipios.findAll", query = "SELECT m FROM Municipios m"),
    @NamedQuery(name = "Municipios.findByIdMunicipio", query = "SELECT m FROM Municipios m WHERE m.idMunicipio = :idMunicipio"),
    @NamedQuery(name = "Municipios.findByCep", query = "SELECT m FROM Municipios m WHERE m.cep = :cep"),
    @NamedQuery(name = "Municipios.findByCodigoDne", query = "SELECT m FROM Municipios m WHERE m.codigoDne = :codigoDne"),
    @NamedQuery(name = "Municipios.findByCodigoIbge", query = "SELECT m FROM Municipios m WHERE m.codigoIbge = :codigoIbge"),
    @NamedQuery(name = "Municipios.findByFlagAtivo", query = "SELECT m FROM Municipios m WHERE m.flagAtivo = :flagAtivo"),
    @NamedQuery(name = "Municipios.findByNome", query = "SELECT m FROM Municipios m WHERE m.nome = :nome")})

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idMunicipio", scope= Municipios.class)    
public class Municipios implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    private Long idMunicipio;
    @Size(max = 8)
    private String cep;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    private String codigoDne;
    @Size(max = 7)
    private String codigoIbge;
    private Integer flagAtivo;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 72)
    private String nome;


    @JoinColumn()
    @ManyToOne(optional = false)
    private Ufs idUf;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idMunicipio")


    private Collection<Bairros> bairrosCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idMunicipio")
    private Collection<PessoasEnderecos> pessoasEnderecosCollection;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idMunicipio")
    private Collection<Logradouros> logradourosCollection;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idMunicipio")
    private Collection<Entidades> entidadesCollection;

    public Municipios() {
    }

gets and sets ... 


}

ufs class

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idUf", scope= Ufs.class)  
public class Ufs implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    private Long idUf;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2)
    private String codigoDne;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 72)
    private String nome;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2)
    private String sigla;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idUf")
    private Collection<Municipios> municipiosCollection;
    @JoinColumn()
    @ManyToOne(optional = false)
    private Paises idPais;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idUf")
    private Collection<PessoasEnderecos> pessoasEnderecosCollection;

    public Ufs() {
    }

gets and sets ... 

}
  • It would be possible to provide a mvce? The way it is is very difficult to simulate your problem, this column in bank would not be "garbage" of changes in entities?

  • @Murillogoulart I’ll arrange it, when I’m ready I’ll let you know

  • Providing the description of your table can also be useful...

  • Print the Districts object on the console to see if it’s coming to the Controller with the values filled in correctly.

  • I believe that the column in question referred to by the postgres is either without an informed value or the type of data informed must be incompatible.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.