简体   繁体   中英

Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to

I am still working on my h:selectOneMenu and I am able to produce a good list of values but the converter is not doing something right and I can not figure out the problem.
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to com.ray.adtf.jpa.Gridmaster

THANK YOU for any help!

xhtml:

<h:selectOneMenu id="mypick"
            converter="#{categoryConverterBean}"
            value="#{gridMaster_backing.pickedGrid}" 
            title="Test" >
            <f:selectItems value="#{gridMaster_backing.gridList}" var="prog"       itemValue="#{prog.gridid}" itemLabel="#{prog.gridid} - #{prog.program} - #{prog.project} - #{prog.ci}" />
        </h:selectOneMenu>

jpa:

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
/**
 * The persistent class for the GRIDMASTER database table.
 * 
 */
@Entity
@NamedQuery(name="Gridmaster.findAll", query="SELECT g FROM Gridmaster g")
public class Gridmaster implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private long gridid;

    private String ci;


    @Column(name="\"PROGRAM\"")
    private String program;

    private String project;

    public Gridmaster() {
    }

    public Long getGridid() {
        return this.gridid;
    }

    public void setGridid(Long gridid) {
        this.gridid = gridid;
    }

    public String getCi() {
        return this.ci;
    }

    public void setCi(String ci) {
        this.ci = ci;
    }


    public String getProgram() {
        return this.program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

    public String getProject() {
        return this.project;
    }

    public void setProject(String project) {
        this.project = project;
    }

Converter:

import javax.faces.bean.ManagedBean;
import com.ray.adtf.jpa.Gridmaster;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
//You must annotate the converter as a managed bean, if you want to inject
//anything into it, like your persistence unit for example.
@ManagedBean(name = "categoryConverterBean") 
@FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter {

 @PersistenceContext(unitName = "Test-Persistence")
 // I include this because you will need to 
 // lookup  your entities based on submitted values
 private transient EntityManager em;  

 @Override


    public Object getAsObject(FacesContext ctx, UIComponent component,
             String value) {
       // This will return the actual object representation
       // of your Category using the value (in your case 52) 
       // returned from the client side
       return em.find(Gridmaster.class, new Long(value)); 
     }

     @Override
     public String getAsString(FacesContext fc, UIComponent uic, Object o) {
         //This will return view-friendly output for the dropdown menu
         return ((Gridmaster) o).getGridid().toString();
     }
    }

backing bean:

public Long pickedGrid; 

    public Long getPickedGrid() {
        return pickedGrid;
    }

    public void setPickedgrid(Long pickedGrid) {
        this.pickedGrid = pickedGrid;
    }


Stack Trace
javax.servlet.ServletException: java.lang.Long cannot be cast to com.ray.adtf.jpa.Gridmaster
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

Can you try : long type into int type

@Id
private int gridid;

public int getGridid() {
    return this.gridid;
}

public void setGridid(int gridid) {
    this.gridid = gridid;
}

When you bind a Gridmaster property to the selectOneMenu , store it equally as object in the backing bean:

public Gridmaster pickedGrid; 

public Gridmaster getPickedGrid() {
    return pickedGrid;
}

public void setPickedgrid(Gridmaster pickedGrid) {
    this.pickedGrid = pickedGrid;
}

because your converter does the job of converting the ID to an object so you don't have to store an ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM