Galera, eu sei que essa dúvida já tá batida, mas acredito que meu problema seja um pouco fora do comum…
O que acontece é que quando seleciono um município X e mando cadastrar, ele cadastra numa boa, mas quando seleciono um município Y não cadastra.
Tipo, to lá na página de cadastro e seleciono o município Xapuri (Acre) e mando cadastrar/atualizar e ele vai numa boa, mas se eu selecionar a cidade Rio Branco (Acre) ele não cadastra, dá erro de validação.
Segue abaixo alguns trechos de como está minha implementação:
Modelo (com hashCode e equals sobrescritos):
@Entity
@Table(name = "ger_tb_cidade", catalog = "geral", uniqueConstraints = {
@UniqueConstraint(columnNames = { "fk_id_estado", "nm_cidade" }),
@UniqueConstraint(columnNames = "cd_ibge") })
@SuppressWarnings("serial")
@TypeDef(name = "flagEnum", typeClass = PgEnumUserType.class, parameters =
{@Parameter(name = "enumClassName", value = "inforcoop.seiam.modelo.tipo.Flag")})
public class GerTbCidade implements java.io.Serializable {
private Integer pkIdCidade;
private GerTbEstado gerTbEstado;
private String cdIbge;
private String nmCidade;
private Flag flRemovido;
private Set<CliTbEmpreendimento> cliTbEmpreendimentos = new HashSet<CliTbEmpreendimento>(
0);
private Set<GerTbPessoa> gerTbPessoas = new HashSet<GerTbPessoa>(0);
public GerTbCidade() {
}
public GerTbCidade(GerTbEstado gerTbEstado, String cdIbge, String nmCidade,
Flag flRemovido) {
this.gerTbEstado = gerTbEstado;
this.cdIbge = cdIbge;
this.nmCidade = nmCidade;
this.flRemovido = flRemovido;
}
public GerTbCidade(GerTbEstado gerTbEstado, String cdIbge, String nmCidade,
Flag flRemovido, Set<CliTbEmpreendimento> cliTbEmpreendimentos,
Set<GerTbPessoa> gerTbPessoas) {
this.gerTbEstado = gerTbEstado;
this.cdIbge = cdIbge;
this.nmCidade = nmCidade;
this.flRemovido = flRemovido;
this.cliTbEmpreendimentos = cliTbEmpreendimentos;
this.gerTbPessoas = gerTbPessoas;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "pk_id_cidade", unique = true, nullable = false)
public Integer getPkIdCidade() {
return this.pkIdCidade;
}
public void setPkIdCidade(Integer pkIdCidade) {
this.pkIdCidade = pkIdCidade;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_id_estado", nullable = false)
public GerTbEstado getGerTbEstado() {
return this.gerTbEstado;
}
public void setGerTbEstado(GerTbEstado gerTbEstado) {
this.gerTbEstado = gerTbEstado;
}
@Column(name = "cd_ibge", unique = true, nullable = true, columnDefinition = "character(7)")
public String getCdIbge() {
return this.cdIbge;
}
public void setCdIbge(String cdIbge) {
this.cdIbge = cdIbge;
}
@Column(name = "nm_cidade", nullable = false, length = 70)
public String getNmCidade() {
return this.nmCidade;
}
public void setNmCidade(String nmCidade) {
this.nmCidade = nmCidade;
}
@Column(name = "fl_removido", nullable = false, columnDefinition = "geral.flag DEFAULT '0'") @Type(type = "flagEnum")
public Flag getFlRemovido() {
return this.flRemovido;
}
public void setFlRemovido(Flag flRemovido) {
this.flRemovido = flRemovido;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "gerTbCidade")
public Set<CliTbEmpreendimento> getCliTbEmpreendimentos() {
return this.cliTbEmpreendimentos;
}
public void setCliTbEmpreendimentos(
Set<CliTbEmpreendimento> cliTbEmpreendimentos) {
this.cliTbEmpreendimentos = cliTbEmpreendimentos;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "gerTbCidade")
public Set<GerTbPessoa> getGerTbPessoas() {
return this.gerTbPessoas;
}
public void setGerTbPessoas(Set<GerTbPessoa> gerTbPessoas) {
this.gerTbPessoas = gerTbPessoas;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((pkIdCidade == null) ? 0 : pkIdCidade.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GerTbCidade other = (GerTbCidade) obj;
if (pkIdCidade == null) {
if (other.pkIdCidade != null)
return false;
} else if (!pkIdCidade.equals(other.pkIdCidade))
return false;
return true;
}
}
Converter da Classe:
@FacesConverter(forClass = GerTbCidade.class)
public class GerTbCidadeConverter implements Converter{
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String valor) {
if(valor==null)
return null;
try{
GerTbCidadeDAO dao = new GerTbCidadeDAO(JpaUtil.getEntityManager());
return dao.busca(Integer.parseInt(valor));
}catch(Exception e){
return "";
}
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
if(obj==null)
return null;
try{
GerTbCidade o = (GerTbCidade) obj;
return "" + o.getPkIdCidade();
}catch(Exception e){
return "";
}
}
}
Cabeçalho da minha página:
<?xml version="1.0" encoding="UTF-8"?>
selectOneMenu:
<h:outputLabel value="Cidade: " for="fk_id_cidade">
<em>*</em>
</h:outputLabel>
<p:selectOneMenu id="fk_id_cidade"
value="#{gerTbPessoaControle.gerTbPessoa.gerTbCidade}"
required="true" requiredMessage="#{msgs.msgCampoObrigatorio}">
<f:selectItem itemLabel="#{msgs.lbSelecioneOpacao}" />
<f:selectItems value="#{gerTbPessoaControle.selectItemCidadeByEstado}" />
<f:converter converterId="gerTbCidadeConverter" />
</p:selectOneMenu>
<p:message for="fk_id_cidade" />
<br />
Só repetindo que para algumas cidades ele cadastra/atualiza na boa e para outros dá erro de validação.
Já conferi no banco, tá td cadastrado certo, já tentei ver se segue alguma lógica a questão das cidades que dão certo e as que não dão certo, mas não existe um padrão.
Pensei que pudesse ser alguma coisa relacionada a carater especial mas não é pois ele funfa numa boa algumas cidades com caracter especial, enfim… Parece coisa do além, ou claro, pode ser a mais pura ignorância minha! 