Question

I want to get the instance of the Entity from SelectOneMenu so i can assign the entity variables to some other method. But it is pointing to null.

xhtml code

<h:selectOneMenu value="#{statusReport.projectDetID}" converter="ObjectStringConv" onchange="#{statusReport.retrieveReport()}" >
                <f:selectItems value="#{statusReport.listOfProjectDetail}"
                    var="projectDetail" itemLabel="#{projectDetail.project}              #{projectDetail.startDate}   -    #{projectDetail.endDate}"
                    itemValue="#{projectDetail}" noSelectionValue="Select the Saved Project"/>
            </h:selectOneMenu>

statusReport bean

public class StatusReport implements Serializable {


    private ProjectDetail projectDetID;

    private List<ProjectDetail> listOfProjectDetail;

    public List<ProjectDetail> getListOfProjectDetail() {
        listOfProjectDetail = projectDetailFacade.findAll();
        return listOfProjectDetail;
    }

    public void setListOfProjectDetail(List<ProjectDetail> listOfProjectDetail) {
        this.listOfProjectDetail = listOfProjectDetail;
    }
    public ProjectDetail getProjectDetID() {
        return projectDetID;
    }

    public void setProjectDetID(ProjectDetail projectDetID) {
        this.projectDetID = projectDetID;
    }


    public void retrieveReport(){

        System.out.println(" Processing .....");
        if ( projectDetID == null )
        {
            System.out.println("The object from Select null");
        }
        else
        {
        System.out.println("The object from Select menu" + projectDetID.toString());
        }

        System.out.println("Generated Data:Completed");

}}

ProjectDetail Entity Bean

package com.jira.entity;

import java.io.Serializable;
import javax.persistence.*;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the PROJECT_DETAIL database table.
 * 
 */
@Entity
@Table(name="PROJECT_DETAIL",schema="weeklyrep")

public class ProjectDetail implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="projectdetail_seq")
    @SequenceGenerator(name="projectdetail_seq",schema="weeklyrep",sequenceName="projectdetail_seq", allocationSize=1)
    @Column(name="PDETAIL_ID")
    private long pdetailId;

    private Boolean completed;

    @Temporal( TemporalType.DATE)
    @Column(name="END_DATE")
    private Date endDate;

    private Long project;

    @Temporal( TemporalType.DATE)
    @Column(name="START_DATE")
    private Date startDate;

    //bi-directional many-to-one association to MajorEvent
    @OneToMany(mappedBy="projectDetail",cascade=CascadeType.ALL)
    private List<MajorEvent> majorEvents;
    //bi-directional one-to-one association to ExecSummary
    @OneToOne(mappedBy="projectDetailExec",cascade=CascadeType.ALL)
    private ExecSummary execSummary;
    public ProjectDetail() {
    }

    public long getPdetailId() {
        return this.pdetailId;
    }

    public void setPdetailId(Long pdetailId) {
        this.pdetailId = pdetailId;
    }

    public Boolean getCompleted() {
        return this.completed;
    }

    public void setCompleted(Boolean completed) {
        this.completed = completed;
    }

    public Date getEndDate() {
        return this.endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

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

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

    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public List<MajorEvent> getMajorEvents() {
        return this.majorEvents;
    }

    public void setMajorEvents(List<MajorEvent> majorEvents) {
        this.majorEvents = majorEvents;
    }

    public ExecSummary getExecSummary() {
        return execSummary;
    }

    public void setExecSummary(ExecSummary execSummary) {
        this.execSummary = execSummary;
    }


}

Converter

I don't know if it needs converter, however i don't know how code it.

package com.weeklyreport.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.jira.entity.ProjectDetail;


@FacesConverter(value="ObjectStringConv")
public class ObjectStringConv implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String svalue) {
        System.out.print("String version of object is:" + svalue);
        return "test";
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object ovalue) {

            return ovalue.toString();
        }

    }

Please help me figure this out. Is there a way we get instance of the entity object like this?

Was it helpful?

Solution

Your converter needs to be written that way so that it can convert between ProjectDetail and String based on an unique identifier of ProjectDetail. Usually entities have an id. You need to use this as String value. Here's a kickoff example without any trivial checks like null and instanceof:

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    // Convert ProjectDetail to its unique String representation.
    ProjectDetail projectDetail = (ProjectDetail) value;
    String idAsString = String.valueOf(projectDetail.getId())
    return idAsString;
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    // Convert unique String representation of ProjectDetail back to ProjectDetail object.
    Long id = Long.valueOf(value);
    ProjectDetail projectDetail = someProjectDetailService.find(id);
    return projectDetail;
}

Note that using EJBs in JSF converters (and validators) needs some hackery. See also How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top