pessoal, estou tendo dificuldade pra fazer um cadastro com um relaciomento 1-N com JSF e Hibernate, pois sempre que utilizo o <h:selectOneMenu /> tenho o mesmo erro…
“Erro de validação: o valor não é válido”
gerei as classes de entidade atraves do netbeans, tentei reimplementar o equals e hashCode mas não tive sucesso…
o que pode estar acontecendo?
XHTML
<h:selectOneMenu id="paisClube" value="#{clubeController.clube.pais}" converter="paisConverter" styleClass="select-one-menu">
<f:selectItems value="#{paisController.paisList}" var="pais" itemLabel="#{pais.nome}" itemValue="#{pais}" />
</h:selectOneMenu>
Classe Converter
@FacesConverter( "paisConverter" )
public class PaisConverter implements Converter {
@Override
public Object getAsObject( FacesContext fc, UIComponent uic, String string ) {
PaisDAO paisDAO = DAOFactory.createPaisDAO();
Pais pais = null;
try {
pais = paisDAO.find( Integer.parseInt( string ) );
} catch( NumberFormatException e ) {
e.printStackTrace( System.err );
} catch( HibernateException e ) {
e.printStackTrace( System.err );
}
return pais;
}
@Override
public String getAsString( FacesContext fc, UIComponent uic, Object object ) {
if( object instanceof Pais ) {
Pais pais = ( Pais )object;
return String.valueOf( pais.getIdPais() );
}
return null;
}
}
Classe Clube
package br.com.sisfute.model.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author Victor Guimarães Nunes <victor.gnunes at msn.com>
*/
@Entity
@Table( name = "clube" )
@NamedQueries( {
@NamedQuery( name = "Clube.findAll", query = "SELECT c FROM Clube c" ) } )
public class Clube implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Basic( optional = false )
@Column( name = "id_clube" )
private Integer idClube;
@Basic( optional = false )
@Column( name = "nome" )
private String nome;
@ManyToMany( fetch = FetchType.LAZY, mappedBy = "clubeList" )
private List<Jogador> jogadorList;
@JoinColumn( name = "id_pais", referencedColumnName = "id_pais" )
@ManyToOne( optional = false )
private Pais pais;
// getters e setters
@Override
public int hashCode() {
int hash = 0;
hash += ( idClube != null ? idClube.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 Clube ) ) {
return false;
}
Clube other = ( Clube )object;
if( ( this.idClube == null && other.idClube != null ) || ( this.idClube != null && !this.idClube.equals( other.idClube ) ) ) {
return false;
}
return true;
}
@Override
public String toString() {
return "br.com.sisfute.model.entity.Clube[ idClube=" + idClube + " ]";
}
}
Classe Pais
package br.com.sisfute.model.entity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
*
* @author Victor Guimarães Nunes <victor.gnunes at msn.com>
*/
@Entity
@Table( name = "pais" )
@NamedQueries( {
@NamedQuery( name = "Pais.findAll", query = "SELECT p FROM Pais p" ) } )
public class Pais implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Basic( optional = false )
@Column( name = "id_pais" )
private Integer idPais;
@Basic( optional = false )
@Column( name = "nome" )
private String nome;
@OneToMany( cascade = CascadeType.ALL, mappedBy = "pais" )
private List<Clube> clubeList;
// getters e setters
@Override
public int hashCode() {
int hash = 0;
hash += ( idPais != null ? idPais.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 Pais ) ) {
return false;
}
Pais other = ( Pais )object;
if( ( this.idPais == null && other.idPais != null ) || ( this.idPais != null && !this.idPais.equals( other.idPais ) ) ) {
return false;
}
return true;
}
@Override
public String toString() {
return "br.com.sisfute.model.entity.Pais[ idPais=" + idPais + " ]";
}
}
obrigado pela atenção!