Paolo Dragone
Paolo Dragone

Reputation: 919

PrimeFaces DataTable editing with autocomplete in pojo

I have a page with a datatable which have to display various informations about Mailbox objects. One of these informations is the owner of the Mailbox which is stored by its id in Mailbox object. In output I solved this with a method in backing bean that retrieve the username by the mailbox object. In input I thought to use autocomplete with pojo but I can't exactly realize how do this.

My jsf page:

<p:dataTable id="dataTable" value="#{bean.mailboxes}" var="m" editable="true">
  <!-- other table -->
  <p:column headerText="Owner">
    <p:cellEditor>
      <f:facet name="output">  
        <h:outputText value="#{bean.userByMailbox(m)}" />  
      </f:facet>
      <f:facet name="input">
        <!-- here comes autocomplete -->
      </f:facet>  
    </p:cellEditor>
  </p:column>
</p:dataTable>

And my bean:

public class Bean {

// Other properties and methods

List<Mailbox> mailboxes;

public List<Mailbox> getMailboxes() {
    if (mailboxes == null) {
        Query q = em.createNamedQuery("Mailbox.findAll");
        mailboxes = q.getResultList();
    }

    return mailboxes;
}

public User getUserByMailbox(Mailbox m) {
    Query q = em.createNamedQuery("User.findByUsrId");
    q.setParameter("usrId", m.getUsrId());
    return (User)q.getSingleResult();
}
}

Thank you all!

Upvotes: 0

Views: 1305

Answers (1)

BalusC
BalusC

Reputation: 1108852

Your model is wrong.

In Mailbox, replace

@Column
private Long usrId;

by

@ManyToOne
@JoinColumn(name="usrId")
private User user;

This way you can just use#{m.user} instead of #{bean.userByMailbox(m)}. This way the property is also writable (perhaps you actually got a PropertyNotWritableException while attempting to use this EL expression in <p:autoComplete value>; in the future questions tell that so instead of asking an overly generic question).

Note that this concrete problem has essentially nothing to do with JSF nor <p:autoComplete>.

Upvotes: 2

Related Questions