0 Replies Latest reply on May 26, 2008 9:10 PM by Bolke de Bruin

    selectManyCheckbox with extra InputText: A better way to do this?

    Bolke de Bruin Newbie


      I have a relationship between two entities which is essentially a manytomany relation ship if not for the extra data the relationship can provide. Therefore I have created a connector class.

      in Chemical.java
      @OneToMany(mappedBy="chemical", cascade=CascadeType.ALL)
      private List<ChemicalNoteConnector> notes;
      in ChemicalNoteConnector.java
      private Chemical chemical;
      private ChemicalNote chemicalNote;
      in ChemicalNote.java
      @OneToMany(mappedBy="chemicalNote", cascade=CascadeType.ALL)
      private List<ChemicalNoteConnector> chemicals;

      A normal ManyToMany relation ship is quite easily solved using a selectManyCheckbox for the frontend. However it does not allow for the extra data. To solve this I added

      private List<NoteSelectItem> notes;

      which uses a class derived from SelectItem to contain the relationship. This uses a lot of plumbing code to make it work and just does not seam very efficient to me. Eg:

           public void findNotes() {
                if (chemical == null)
                List<ChemicalNoteConnector> connectedNotes;
                if (entityManager.contains(chemical)) {
                     connectedNotes = 
                          entityManager.createQuery("from ChemicalNoteConnector cnn where cnn.chemical = :chemical")
                          .setParameter("chemical", chemical)
                } else {
                     connectedNotes = new ArrayList<ChemicalNoteConnector>();
                List<ChemicalNote> notesList = entityManager.createQuery("from ChemicalNote c order by c.number asc").getResultList();
                List<NoteSelectItem> items = new ArrayList<NoteSelectItem>();
                for (ChemicalNote n : notesList) {
                     NoteSelectItem item = new NoteSelectItem();
                     for (ChemicalNoteConnector cnn : connectedNotes) {
                          if (cnn.getChemicalNote().getNumber() == n.getNumber()) {
                notes = items;
           private void setNotes() {
                log.info("chemicalManager.setNotes called");
                if (entityManager.contains(chemical)) {
                     for (ChemicalNoteConnector nc : chemical.getNotes()) {
                List<ChemicalNoteConnector> connectors = new ArrayList<ChemicalNoteConnector>();
                for (NoteSelectItem item : notes) {
                     if (!item.isSelected())
                     log.info("Adding connector: " + item.getLabel() + " extra: " + item.getExtra());
                     ChemicalNoteConnector connector = new ChemicalNoteConnector();
                     connector.setChemicalNote((ChemicalNote) item.getValue());
                     // Empty inputtexts are not null
                     if (item.getExtra() != null || !item.getExtra().equals(""))

      Well it works. But I was thinking there should be an easier way as I think the extra data part if a quite common requirement. Does someone have an idea and is willing to share it?