Boa tarde,
Estou com um problema ao implementar um OneToMany com JSF2 + Primefaces.
Tenho a classe Autor e Livro, onde na segunda possui o atributo Autor devidamente mapeado.
Na página de cadastro de Livro, eu carrego um SelectOneMenu com todos os autores do banco de dados.
Ela é carregada normalmente, mas ao tentar salvar algum livro, recebo a seguinte mensagem com foco neste SelectOneMenu de autores: Erro de validação: o valor não é válido.
Já vasculhei a internet de cabo a rabo e vi que podem ser conversores ou hashCode() e Equals().
Já implementei todos e o erro persiste. Faz alguns dias que estou preso nisso e já não sei mais o que tentar.
Entidade Autor
@Entity
@Table
public class Autor implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer idAutor;
private String nomeAutor;
@OneToMany(mappedBy="autor")
private List<Livro> listaLivros;
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((idAutor == null) ? 0 : idAutor.hashCode());
result = prime * result + ((listaLivros == null) ? 0 : listaLivros.hashCode());
result = prime * result + ((nomeAutor == null) ? 0 : nomeAutor.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Autor))
return false;
Autor other = (Autor) obj;
if (idAutor == null)
{
if (other.idAutor != null)
return false;
} else if (!idAutor.equals(other.idAutor))
return false;
if (listaLivros == null)
{
if (other.listaLivros != null)
return false;
} else if (!listaLivros.equals(other.listaLivros))
return false;
if (nomeAutor == null)
{
if (other.nomeAutor != null)
return false;
} else if (!nomeAutor.equals(other.nomeAutor))
return false;
return true;
}
//gets sets e construtures
}
Converter de Autor
@FacesConverter(value = "autorConverter", forClass = Autor.class)
public class AutorConverter implements Converter
{
public Object getAsObject(FacesContext fc, UIComponent ui, String value)
{
Autor retorno = null;
Session session = null;
if (value != null)
{
try
{
session = HibernateUtil.getSessionFactory().openSession();
return retorno = (Autor) session.get(Autor.class, new Integer(value) );
}
catch (Exception e)
{
System.out.println("Erro no converter autor");
}
finally
{
session.close();
}
}
return null;
}
public String getAsString(FacesContext fc, UIComponent ui, Object value)
{
if (value != null)
{
return ( (Autor) value).getIdAutor().toString();
}
return null;
}
Trecho xhtml do SelectOneMenu
<p:outputLabel value="Autor" for="autor" />
<p:selectOneMenu filter="true" value="#{beanLivro.livro.autor}" converter="autorConverter" id="autor">
<f:selectItem itemLabel="Selecione" noSelectionOption="true" />
<f:selectItems value="#{beanLivro.listaAutores}" itemValue="#{a}"
var="a" itemLabel="#{a.nomeAutor}">
</f:selectItems>
</p:selectOneMenu>
Alguém tem alguma ideia do que possa ser?
Grande abraço.