I had this exception in selecting a single row:
javax.faces.FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled or you need to define rowKey attribute
Here is the table:
<h:form>
<p:dataTable id="books" value="#{myCardBean.booksList}" var="book" selectionMode="single"
selection="#{myCardBean.selectedBook}" rowKey="book.id">
<p:ajax event="rowSelect" listener="#{myCardBean.onRowSelect}"/>
<p:ajax event="rowUnselect" listener="#{myCardBean.onRowUnselect}"/>
<p:column>
<f:facet name="header">Book ID</f:facet>
<h:outputText value="#{book.id}"/>
</p:column>
<p:column>
<f:facet name="header">Title</f:facet>
<h:outputText value="#{book.title}"/>
</p:column>
</p:dataTable>
</h:form>
And this is myCardBean :
@ManagedBean
@ViewScoped
@Component
public class MyCardBean implements Serializable {
@Autowired
private BookDao bookDao;
private List<Book> booksList;
private Book selectedBook;
@PostConstruct
public void init() {
userCard();
}
@Transactional
public List<Book> userCard() {
booksList = bookDao.findAllBooks();
System.out.println("all Books size: " + booksList.size() + " books list type: " + booksList); // 3 , list of Object
return booksList;
}
public void onRowSelect(SelectEvent event) {
System.out.println("row selected, " + event.getObject());
}
public void onRowUnselect(UnselectEvent event) {
}
//getter/setter for selectedBook and bookList
And this is Book model class:
public class Book implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String title;
//...
I defined rowKey and Selection and selectionMode in datatable
I have no idea why should i got this error?!
should id be String instead of Integer?
@Transactionalin managed beans? It strictly belongs to the service layer (where you definedBookDao). - Tiny