JSON parsing error: there was already a POJO for id

I already saw some question about this topic, but I could not apply it to my case, because I tried to apply them, and the error continued. so I came to expose my situation.

I read a few comments about this and said that if I changed the name of my idEntidade column in the Entidade class, I could succeed. But I can not change the database.

I try to put scope= Distritos.classin mine @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idDistrito"), but it did not work.

I need help.

I am trying to store data in my database using spring MVC with Angular. When I click save data, I got this error

2017-09-29 13:28:02.126  WARN 2716 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]] (through reference chain: digifred.global.model.Distritos["idEntidade"]->digifred.global.model.Entidades["idEntidade"])
2017-09-29 13:28:02.126  WARN 2716 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]] (through reference chain: digifred.global.model.Distritos["idEntidade"]->digifred.global.model.Entidades["idEntidade"])
2017-09-29 13:46:14.096  INFO 2716 --- [      Thread-46] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53b2ae5: startup date [Fri Sep 29 13:23:36 BRT 2017]; root of context hierarchy

I have a class

@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")        
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;

  gets and sets .. 
}

Entidades Class

@Entity
@Table(name = "entidades", schema="glb")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Entidades.findAll", query = "SELECT e FROM Entidades e"),
    @NamedQuery(name = "Entidades.findByIdEntidade", query = "SELECT e FROM Entidades e WHERE e.idEntidade = :idEntidade"),
    @NamedQuery(name = "Entidades.findByBairro", query = "SELECT e FROM Entidades e WHERE e.bairro = :bairro"),
    @NamedQuery(name = "Entidades.findByBrasao", query = "SELECT e FROM Entidades e WHERE e.brasao = :brasao"),
    @NamedQuery(name = "Entidades.findByCnpj", query = "SELECT e FROM Entidades e WHERE e.cnpj = :cnpj"),
    @NamedQuery(name = "Entidades.findByComplemento", query = "SELECT e FROM Entidades e WHERE e.complemento = :complemento"),
    @NamedQuery(name = "Entidades.findByEmail", query = "SELECT e FROM Entidades e WHERE e.email = :email"),
    @NamedQuery(name = "Entidades.findByLogradouro", query = "SELECT e FROM Entidades e WHERE e.logradouro = :logradouro"),
    @NamedQuery(name = "Entidades.findByNome", query = "SELECT e FROM Entidades e WHERE e.nome = :nome"),
    @NamedQuery(name = "Entidades.findByNumero", query = "SELECT e FROM Entidades e WHERE e.numero = :numero"),
    @NamedQuery(name = "Entidades.findBySigla", query = "SELECT e FROM Entidades e WHERE e.sigla = :sigla"),
    @NamedQuery(name = "Entidades.findByTelefone", query = "SELECT e FROM Entidades e WHERE e.telefone = :telefone")})


@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idEntidade")  
public class Entidades implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    private Long idEntidade;
    @Size(max = 50)
    private String bairro;
    private BigInteger brasao;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 14)
    private String cnpj;
    @Size(max = 20)
    private String complemento;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="E-mail inválido")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 30)
    private String email;
    @Size(max = 50)
    private String logradouro;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 70)
    private String nome;
    @Size(max = 20)
    private String numero;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 4)
    private String sigla;
    @Size(max = 12)
    private String telefone;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<EntidadesAdministradores> entidadesAdministradoresCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "entidades")
    private Collection<Bairros> bairrosCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<PessoasDeficiencias> pessoasDeficienciasCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<PessoasEnderecos> pessoasEnderecosCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<EntidadesLicencas> entidadesLicencasCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<PessoasContatos> pessoasContatosCollection;
    @OneToMany(mappedBy = "idEntidade")
    private Collection<Logradouros> logradourosCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<Pessoas> pessoasCollection;
    @JoinColumn()
    @ManyToOne(optional = false)
    private Municipios idMunicipio;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntidade")
    private Collection<PessoasCaracteristicas> pessoasCaracteristicasCollection;

    public Entidades() {
    }
          .... gets and sets . . 

    }

Class DistritosController

@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);
    }


}

enter image description here

enter image description here

enter image description here

+6
1

:

1.

@JsonIdentityInfo , - ObjectIdGenerators.PropertyGenerator.class.

: ; . - , . ; , , , (.. )

Distritos:

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

Entidades:

@JsonIdentityInfo(scope = Entidades.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "idEntidade")

2.

@JoinColumn()
@ManyToOne
private Entidades idEntidade;

-

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Long idEntidade;

Distritos ( Entidades , ).

, BeanDeserializer ( ) , , id, idEntidade entidade Distritos.

( - Distritos.idMunicipio, Distritos.idUf Collection Entidades). , (, Municipios Ufs) .

+4
source

All Articles