无法将字符串转换为Object

时间:2013-08-03 23:50:08

标签: java-ee jpa

使用我的webapp,我遇到了很难(对我而言)。这是玻璃鱼的输出:

      ATTENTION: EJB5184:A system exception occurred during an invocation on EJB           DepartementFacade, method: public jpa.Departement    ejb.DepartementFacade.getDiscountCodeSFromStringCode(java.lang.String)
      ATTENTION: javax.ejb.EJBException
at    com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
at $Proxy289.getDiscountCodeSFromStringCode(Unknown Source)
at ejb.__EJB31_Generated__DepartementFacade__Intf____Bean__.getDiscountCodeSFromStringCode(Unknown Source)
at ManagaedBean.AgentBean$1.getAsObject(AgentBean.java:120)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:293)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at     com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.Character for parameter idDept with expected type of class java.lang.String from query string SELECT d FROM Departement d WHERE d.idDept = :idDept.

我真的需要你的帮助 这是Xhtml文件:

   <h:selectOneMenu id="idDept" value="#{agentBean.agentministere.idDept}" title="IdDept" converter="#{agentBean.departementConverter}" >

                                        <f:selectItems value="#{agentBean.allDepartements}"/>  
    </h:selectOneMenu>

Managed bean:

     package ManagaedBean;

    import DAO.Manager;
    import ejb.DepartementFacade;
    import java.io.Serializable;
    import java.util.List;
    import javax.ejb.EJB;
    import javax.enterprise.context.SessionScoped;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.faces.model.SelectItem;
    import javax.inject.Named;
    import jpa.Agentministere;
    import jpa.Departement;


  @Named(value = "agentBean")

  @SessionScoped
  public class AgentBean implements Serializable {
@EJB
private DepartementFacade departementFacade;


@EJB
private Manager manager;

private List <Agentministere> agentministeres;
private Agentministere agentministere;
 List <SelectItem> listDepartements;
/**
 * Creates a new instance of AgentBean
 */
public AgentBean() {
}



      public void setAgentministeres(List<Agentministere> agentministeres) {
    this.agentministeres = agentministeres;
}


      public List<Agentministere> getAgentministeres() {
    agentministeres=manager.getAllAgents();
    return agentministeres;
   }




      public Agentministere getAgentministere() {
    return agentministere;
    }

     public void setAgentministere(Agentministere agentministere) {
    this.agentministere = agentministere;
    }




      public String enregistrerAgent() throws  Exception{
manager.agentCreer(agentministere);
    return "AgentList";
    }

     public String ajoutNouveauUtilisateur (){
     agentministere =new Agentministere();
     return "Ajout_Agent";
     } 


      public List<Departement> getAllDepartements() {
    return manager.getDepartements();
     }   


     //i tried to use this converter i don't know if it cause the problem

     public Converter getDepartementConverter() {  
   return departementConverter;  
     }  

      private Converter departementConverter = new Converter() {  

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
  System.out.println("Dans GET AS Object : " + value);
  char code = value.charAt(0);
  Departement dc = departementFacade.getDiscountCodeSFromStringCode(""+code);
  return dc;
     }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
    Departement dc = (Departement) value;
    return dc.getIdDept()+ " : " +dc.getLibelleDept() ;
    }
    };  



     public javax.faces.model.SelectItem[] getDepartements()
{
    SelectItem[] options = null;
    List<Departement> departements = manager.getDepartements();
    if (departements!= null && departements.size() > 0)
    {
        int i = 0;
        options = new SelectItem[departements.size()];
        for (Departement dc : departements)
        {
            options[i++] = new SelectItem(dc.getIdDept());
        }
    }
    return options;
}

}

会话bean:

  @Stateless
  @LocalBean

     public class Manager {
@PersistenceContext(unitName = "NvApplication_PArc-ejbPU")
private EntityManager em;



@EJB
private AgentministereFacade agentministereFacade;




public List <Agentministere> getAllAgents() {
return agentministereFacade.findAll();
}




public void agentCreer( Agentministere agentministere) {
    agentministereFacade.create(agentministere);


    }

DepartemetFacade:

  package ejb;

   import javax.ejb.Stateless;
   import javax.persistence.EntityManager;
   import javax.persistence.PersistenceContext;
   import javax.persistence.Query;
   import jpa.Departement;


@Stateless
public class DepartementFacade extends AbstractFacade<Departement> {
@PersistenceContext(unitName = "NvApplication_PArc-ejbPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public DepartementFacade() {
    super(Departement.class);
}

请帮助我不明白问题的原因,thx ^^

   public Departement getDiscountCodeSFromStringCode(String idDept) 
{
    System.out.println("JE CHERCHE Id Dept " + idDept);
    Query query = em.createNamedQuery("Departement.findByIdDept");
    query.setParameter("idDept", new Character(idDept.charAt(0)));
    return (Departement) query.getSingleResult();
}

}

1 个答案:

答案 0 :(得分:2)

  
    

引起:java.lang.IllegalArgumentException:您试图为参数idDept设置类型类java.lang.Character的值,其中包含来自查询字符串的类java.lang.String的预期类型SELECT d FROM Departement d WHERE d .idDept =:idDept。

  

很明显应该有String而不是Character

试试这个:

query.setParameter("idDept", idDepth);

getAsObject包含以下行

char code = value.charAt(0);

我确信你不必在getDiscountCodeSFromStringCode

中重复它