JPA entity generating column with double name id_municipio_id_municipio

0

I have an entity called districts that contains a relationship with the municipalities table. When I generate the database the table districts creates the field id_municipio_id_municipio being that was to be generated only id_municipality.

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

    }
}

class Municipalities

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


    }
    
asked by anonymous 28.09.2017 / 15:34

1 answer

1

What he is doing is putting together the mappings, you in the Districts class are writing:

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

Then, it generates the first id_municipality. In the Municipios class you are declaring the id as follows:

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

Generating a second id_municipio, at this moment to generate the mapping column in the Districts to which municipality it is associated, it generates the column id_municipio_id_municipio, joining all this data.

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

    
28.09.2017 / 15:50