JPA entity generating column with double name id_municipio_id_municipio

Asked

Viewed 83 times

0

I have an entity called districts that contains a relationship with the municipality table. When I run the database the districts table creates the id_municipio_id_municipio field and it was to be generated only id_municipio.

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")
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() {
    }

    public Distritos(Long idDistrito) {
        this.idDistrito = idDistrito;
    }

    public Distritos(Long idDistrito, String nome) {
        this.idDistrito = idDistrito;
        this.nome = nome;
    }

    public Long getIdDistrito() {
        return idDistrito;
    }

    public void setIdDistrito(Long idDistrito) {
        this.idDistrito = idDistrito;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCodigoDne() {
        return codigoDne;
    }

    public void setCodigoDne(String codigoDne) {
        this.codigoDne = codigoDne;
    }

    public Integer getFlagAtivo() {
        return flagAtivo;
    }

    public void setFlagAtivo(Integer flagAtivo) {
        this.flagAtivo = flagAtivo;
    }

    public Entidades getIdEntidade() {
        return idEntidade;
    }

    public void setIdEntidade(Entidades idEntidade) {
        this.idEntidade = idEntidade;
    }

    public Municipios getIdMunicipio() {
        return idMunicipio;
    }

    public void setIdMunicipio(Municipios idMunicipio) {
        this.idMunicipio = idMunicipio;
    }

    public Ufs getIdUf() {
        return idUf;
    }

    public void setIdUf(Ufs idUf) {
        this.idUf = idUf;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idDistrito != null ? idDistrito.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Distritos)) {
            return false;
        }
        Distritos other = (Distritos) object;
        if ((this.idDistrito == null && other.idDistrito != null) || (this.idDistrito != null && !this.idDistrito.equals(other.idDistrito))) {
            return false;
        }
        return true;
    }

    }
}

municipios class

@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")
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() {
    }

    public Municipios(Long idMunicipio) {
        this.idMunicipio = idMunicipio;
    }

    public Municipios(Long idMunicipio, String codigoDne, String nome) {
        this.idMunicipio = idMunicipio;
        this.codigoDne = codigoDne;
        this.nome = nome;
    }

    public Long getIdMunicipio() {
        return idMunicipio;
    }

    public void setIdMunicipio(Long idMunicipio) {
        this.idMunicipio = idMunicipio;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getCodigoDne() {
        return codigoDne;
    }

    public void setCodigoDne(String codigoDne) {
        this.codigoDne = codigoDne;
    }

    public String getCodigoIbge() {
        return codigoIbge;
    }

    public void setCodigoIbge(String codigoIbge) {
        this.codigoIbge = codigoIbge;
    }

    public Integer getFlagAtivo() {
        return flagAtivo;
    }

    public void setFlagAtivo(Integer flagAtivo) {
        this.flagAtivo = flagAtivo;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Ufs getIdUf() {
        return idUf;
    }

    public void setIdUf(Ufs idUf) {
        this.idUf = idUf;
    }

    @XmlTransient
    public Collection<Bairros> getBairrosCollection() {
        return bairrosCollection;
    }

    public void setBairrosCollection(Collection<Bairros> bairrosCollection) {
        this.bairrosCollection = bairrosCollection;
    }

    @XmlTransient
    public Collection<PessoasEnderecos> getPessoasEnderecosCollection() {
        return pessoasEnderecosCollection;
    }

    public void setPessoasEnderecosCollection(Collection<PessoasEnderecos> pessoasEnderecosCollection) {
        this.pessoasEnderecosCollection = pessoasEnderecosCollection;
    }

    @XmlTransient
    public Collection<Logradouros> getLogradourosCollection() {
        return logradourosCollection;
    }

    public void setLogradourosCollection(Collection<Logradouros> logradourosCollection) {
        this.logradourosCollection = logradourosCollection;
    }

    @XmlTransient
    public Collection<Entidades> getEntidadesCollection() {
        return entidadesCollection;
    }

    public void setEntidadesCollection(Collection<Entidades> entidadesCollection) {
        this.entidadesCollection = entidadesCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idMunicipio != null ? idMunicipio.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Municipios)) {
            return false;
        }
        Municipios other = (Municipios) object;
        if ((this.idMunicipio == null && other.idMunicipio != null) || (this.idMunicipio != null && !this.idMunicipio.equals(other.idMunicipio))) {
            return false;
        }
        return true;
    }


    }

1 answer

1


What he’s doing is piecing together the maps, you in the Districts class are writing:

@JoinColumn()
@ManyToOne(optional = false)
private Municipios idMunicipio;

then it generates the first id_municipio. In the Municipality class you are declaring id as follows:

@Id
@Basic(optional = false)
@NotNull
private Long idMunicipio;

generating a second id_municipio, at this time to generate the mapping column in the districts to which the municipality is associated, it generates the column id_municipio_id_municipio, gathering all these data.

A very good tip about java encoding pattern, is you declare your class name in the singular: District and Municipality.

Browser other questions tagged

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