简体   繁体   中英

DataModel must implement org.primefaces.model.SelectableDataModel exception using primefaces filter

Want to filter the resultset of a query which is displayed through a datatable. The row selection, row sorting clicking on column headers and the pagination functionnalities of the datatable works fine. When I add the primefaces filtering functionnality to the datatable, I then run into the

javax.faces.FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

Object Entity:

@Entity
@Table(name="Customer", 
       uniqueConstraints={@UniqueConstraint(columnNames={"ID"})})

public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID", nullable=false, unique=true, length=11)
    private Integer id;

    @Column(name="LASTNAME", length=40, nullable=false)
    private String lastName;

    @Column(name="FIRSTNAME", length=30, nullable=true)
    private String firstName;
....
}

Managed Bean:

@ManagedBean(name = "customerController")
@ViewScoped

public class CustomerController implements Serializable {

    private static final long serialVersionUID = 1L;
    private Customer selectedCustomer = new Customer();
    private List<Customer> customers = new ArrayList<Customer>();
    private String  message;

    public CustomerController() {
    }

    @PostConstruct
    void init() {        
        CustomerDAO custDAO = new CustomerDAO();
        customers = custDAO.getAllCustomers(); 

        // select first row
        if (customers != null) selectedCustomer=customers.get(0);
    }

    public void onRowSelect(SelectEvent event) {  
        message = "";
    }  

    public void onRowUnselect(UnselectEvent event) {  
        message = "";
    } 

    // getters and setters
    ...
}

Facelet:

<ui:define name="contentPart1" >            
    <h:form id="contentPart1Form">
        <p:dataTable id="singleSelection" var="customer" value="#{customerController.customers}" rowKey="#{customer.id}" 
            selection="#{customerController.selectedCustomer}" selectionMode="single" paginator="true" rows="10">
            <p:ajax event="rowSelect" listener="#{customerController.onRowSelect}" />

            <p:column headerText="#{msg['customerCRUD.labelIdentifier']}" style="width:15%;">
                <h:outputText value="#{customer.id}" readonly="#{facesContext.currentPhaseId.ordinal eq 6}"/>
            </p:column>
            <p:column headerText="#{msg['customerCRUD.labelFirstName']}" sortBy="#{customer.firstName}" style="width:30%;">
                <h:outputText value="#{customer.firstName}" />
            </p:column>
            <p:column headerText="#{msg['customerCRUD.labelLastName']}"  filterBy="#{customer.lastName}" filterMatchMode="contains" 
                sortBy="#{customer.lastName}">
                <h:outputText value="#{customer.lastName}" />
            </p:column>

            <f:facet name="footer">
                <h:outputText value=" "/>
            </f:facet>                  
        </p:dataTable>              
    </h:form>
</ui:define>

After hours of investigation, I finally realized the object entity on which I was appliyng the filter to was not serializable. The resolution was to inherit the object entity from the Serialization class .

@Entity
@Table(name="Customer", 
       uniqueConstraints={@UniqueConstraint(columnNames={"ID"})})

public class Customer implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID", nullable=false, unique=true, length=11)
    private Integer id;

    @Column(name="LASTNAME", length=40, nullable=false)
    private String lastName;

    @Column(name="FIRSTNAME", length=30, nullable=true)
    private String firstName;
....
}

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