SelectOneMenu

2 respostas
surfzera

Pessoal estou precisando de ajuda. Estou com problemas a tentar alterar um valor utilizando o SelectOneMenu. Alguem poderia me ajudar?

Segue código:

<h:selectOneMenu id=“perfil” value="#{ManterPerfil.perfil.id}" >
<f:selectItems value="#{ManterPerfil.listar}" />
</h:selectOneMenu>

PerfilControle

public class PerfilControle {

private Perfil perfil = new Perfil();
  

public List&lt;SelectItem&gt; getlistar() throws Exception{
    PerfilDAO ad = new PerfilDAO();
     List perfis = ad.listar();
     ArrayList&lt;SelectItem&gt; listaPerfis = new ArrayList&lt;SelectItem&gt;();
     for (int i=0; i&lt;perfis.size(); i++) {
        SelectItem perfil1 = new SelectItem();
        Perfil e = (Perfil) perfis.get(i);
        perfil1.setValue(e.getId());
        perfil1.setLabel(e.getNome());
        listaPerfis.add(perfil1);
    }
return listaPerfis;		
}

public Perfil getPerfil() {
    return perfil;
    
}

public void setPerfil(Perfil perfil) {
    this.perfil = perfil;
}

}

Entidade Perfil

public class Perfil implements Serializable{

private Integer id;

private String nome;

private List perfil;
public Perfil(){
    
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getNome() {
    return nome;
}

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

public List getPerfil() {
    return perfil;
}

public void setPerfil(List perfil) {
    this.perfil = perfil;
}

}
>

Erro Ocorrido

javax.servlet.ServletException: #{ManterUsuario.alterar}: javax.faces.el.EvaluationException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)

root cause

javax.faces.FacesException: #{ManterUsuario.alterar}: javax.faces.el.EvaluationException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:78)

javax.faces.component.UICommand.broadcast(UICommand.java:312)

javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)

javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)

com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)

com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)

javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

root cause

javax.faces.el.EvaluationException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:130)

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)

javax.faces.component.UICommand.broadcast(UICommand.java:312)

javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)

javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)

com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)

com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)

javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

root cause

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)

org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)

org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)

dao.UsuarioDAO.alterar(UsuarioDAO.java:44)

controle.UsuarioControle.alterar(UsuarioControle.java:61)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

java.lang.reflect.Method.invoke(Method.java:585)

com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)

javax.faces.component.UICommand.broadcast(UICommand.java:312)

javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)

javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)

com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)

com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)

javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

root cause

java.sql.BatchUpdateException: Entrada em lote 0 update SMFTB001_USUARIO set LOGIN_SMFTB001=Luiz Antoniodsd, FK_SMFTB002=NULL where PK_SMFTB001=96 foi abortada. Chame getNextException para ver a causa.

org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2516)

org.postgresql.core.v3.QueryExecutorImpl$1.handleError(QueryExecutorImpl.java:399)

org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1314)

org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)

org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2578)

org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)

org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)

org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)

dao.UsuarioDAO.alterar(UsuarioDAO.java:44)

controle.UsuarioControle.alterar(UsuarioControle.java:61)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

java.lang.reflect.Method.invoke(Method.java:585)

com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)

javax.faces.component.UICommand.broadcast(UICommand.java:312)

javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)

javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)

com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)

com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)

javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

root cause

org.postgresql.util.PSQLException: ERRO: valor nulo na coluna FK_SMFTB002 viola a restrição £o-nula

org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1531)

org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1313)

org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)

org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2578)

org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)

org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)

org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)

org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)

org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)

org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)

dao.UsuarioDAO.alterar(UsuarioDAO.java:44)

controle.UsuarioControle.alterar(UsuarioControle.java:61)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

java.lang.reflect.Method.invoke(Method.java:585)

com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)

com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)

javax.faces.component.UICommand.broadcast(UICommand.java:312)

javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)

javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)

com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)

com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)

com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)

javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

note

2 Respostas

D

Fala cara Tudo bem ???

Não entendi bem o que você quer…

O que faz o seu converter ???

Não vi nada de onchange, ou alguma ação na sua tag, se for isso use o a4j:support com o evento onchange e coloque a sua ação neste comando.

Por se tratar de ajax, não esqueça de passar a propriedade para reiderizar.

Se não for esse seu problema, especifique mais para que todos possam ajuda-lo.

abs,

surfzera

Eu tirei o converter na verdade era pra fazer um teste.

O negocio é o seguinte eu tenho uma ligação Many-to-One Usuario-Perfil - Quando eu listo o perfil que é uma String eu gostaria de retornar para um Int com o codigo para inserir no banco. e não estou conseguindo.

Usuario.hbm

smftb001_usuario_pk_smftb001_seq smftb002_perfil_pk_smftb002_seq

Obs: os &quot são porque o Postgree nao aceita caixa alta.

Entidade Usuario

public class Usuario implements Serializable{

private Integer id;

private String loginUsuario;

private Perfil perfil;
public Usuario(){

}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getLoginUsuario() {
    return loginUsuario;
}

public void setLoginUsuario(String loginUsuario) {
    this.loginUsuario = loginUsuario;
}

public Perfil getPerfil() {
    return perfil;
}

public void setPerfil(Perfil perfil) {
    this.perfil = perfil;
}

}

Controle Usuario

import dao.UsuarioDAO;

import entidade.Usuario;

import java.util.ArrayList;

import javax.faces.component.html.HtmlDataTable;

public class UsuarioControle {

private Usuario usuario = new Usuario();

public Usuario getUsuario() {
    return usuario;
}

public void setUsuario(Usuario usuario) {
    this.usuario = usuario;
}

public UsuarioControle(){
}

private HtmlDataTable table;


public HtmlDataTable getTable() {
    return table;
}

public void setTable(HtmlDataTable table) {
    this.table = table;
}
        
public String novoUsuario() {
    return "novo";
}

public String inserir() throws Exception{
    UsuarioDAO ad = new UsuarioDAO();
    ad.inserir(this.getUsuario());
    return "sucesso";
}

public String update(){
    Usuario usuario1 = getUsuarioFromEditOrDelete();
    setUsuario(usuario1);
    return "editar";
}

public String alterar() throws Exception{
    UsuarioDAO ad = new UsuarioDAO();
    ad.alterar(getUsuario());
    return "sucesso";
}


public Usuario getUsuarioFromEditOrDelete(){
    Usuario usuario1 = (Usuario) table.getRowData();
    return usuario1;
}

public String apagar() throws Exception {
    UsuarioDAO ad = new UsuarioDAO();
    Usuario usuario1 = getUsuarioFromEditOrDelete();
    ad.apagar(usuario1);
    return "sucesso";
}

public ArrayList<Usuario> getlistar() throws Exception{
    UsuarioDAO ad = new UsuarioDAO();
return ad.listar();		
}

}

PerfilDAO

public class PerfilDAO {

public List listar() throws Exception {
    List lista = new ArrayList();
    try {
        SessionFactory sf = new Configuration().configure("hbm/hibernate.cfg.xml").buildSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        Criteria crit = session.createCriteria(Perfil.class);
        lista = crit.list();
        session.flush();
        tx.commit();
        session.close();
    } catch (HibernateException e1) {
        e1.printStackTrace();
        throw e1;
    }
    return lista;
}

}

Criado 24 de julho de 2008
Ultima resposta 24 de jul. de 2008
Respostas 2
Participantes 2