4 Replies Latest reply on Nov 17, 2008 10:15 AM by gregtk

    EntityHome and h:selectManyCheckbox problem

    gregtk

      Hello, guys!!! I can't persist/update checked items with EntityHome. I try to debug but it's doesn't help.


      my settings


      components.xml


       
      <framework:entity-query name="all_groups"
                                  ejbql="select g from ClientGroup g where g.deleted = false order by g.name"
                                  scope="conversation"/>
      


      my Entities



      @Entity
      @Table(name = "CLIENTS")
      public class Client implements Serializable {
      
          private Long id;
          private String surname;
          private String name;
          private String patronymic;
          private String email;
          private List<ClientGroup> clientGroups;
          private boolean subscribed;
      
          @Id
          @GeneratedValue
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          public String getSurname() {
              return surname;
          }
      
          public void setSurname(String surname) {
              this.surname = surname;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String getPatronymic() {
              return patronymic;
          }
      
          public void setPatronymic(String patronymic) {
              this.patronymic = patronymic;
          }
      
          @Email
          @NotNull
          public String getEmail() {
              return email;
          }
      
          public void setEmail(String email) {
              this.email = email;
          }
      
          @ManyToMany(cascade = CascadeType.ALL, mappedBy = "clients", fetch = FetchType.LAZY)
          public List<ClientGroup> getClientGroups() {
              return clientGroups;
          }
      
          public void setClientGroups(List<ClientGroup> clientGroups) {
              this.clientGroups = clientGroups;
          }
      
          public boolean isSubscribed() {
              return subscribed;
          }
      
          public void setSubscribed(boolean subscribed) {
              this.subscribed = subscribed;
          }
      
          @Override
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
      
              Client client = (Client) o;
      
              if (subscribed != client.subscribed) return false;
              if (clientGroups != null ? !clientGroups.equals(client.clientGroups) : client.clientGroups != null)
                  return false;
              if (email != null ? !email.equals(client.email) : client.email != null) return false;
              if (id != null ? !id.equals(client.id) : client.id != null) return false;
              if (name != null ? !name.equals(client.name) : client.name != null) return false;
              if (patronymic != null ? !patronymic.equals(client.patronymic) : client.patronymic != null) return false;
              if (surname != null ? !surname.equals(client.surname) : client.surname != null) return false;
      
              return true;
          }
      
          @Override
          public int hashCode() {
              int result = id != null ? id.hashCode() : 0;
              result = 31 * result + (surname != null ? surname.hashCode() : 0);
              result = 31 * result + (name != null ? name.hashCode() : 0);
              result = 31 * result + (patronymic != null ? patronymic.hashCode() : 0);
              result = 31 * result + (email != null ? email.hashCode() : 0);
              result = 31 * result + (clientGroups != null ? clientGroups.hashCode() : 0);
              result = 31 * result + (subscribed ? 1 : 0);
              return result;
          }
      }
      



      @Entity
      @Table(name = "CLIENT_GROUP")
      @NamedQueries({
              @NamedQuery(name = "findAllgroups", query = "select g from ClientGroup g where g.deleted = false order by g.name ")
      })
      public class ClientGroup implements Serializable {
      
          private Long id;
          private String name;
          private List<Client> clients;
          private boolean deleted;
      
          @Id
          @GeneratedValue
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
          @JoinTable(name = "CLIENTS_TO_GROUPS",
                  joinColumns = @JoinColumn(name = "GROUP_ID"),
                  inverseJoinColumns = @JoinColumn(name = "CLIENT_ID"))
          public List<Client> getClients() {
              return clients;
          }
      
          public void setClients(List<Client> clients) {
              this.clients = clients;
          }
      
          public boolean isDeleted() {
              return deleted;
          }
      
          public void setDeleted(boolean deleted) {
              this.deleted = deleted;
          }
      }
      



      my EntityHome


      @Name("clientHome")
      public class ClientHome extends EntityHome<Client> {
          @In
          EntityManager entityManager;
      
          @Logger
          Log log;
      
          public EntityManager getEntityManager() {
              return entityManager;
          }
      
          @Factory("editClient")
          public Client initClient() {
              return getInstance();
          }
      
          @RequestParameter
          public void setClientId(Long id) throws NewsNotFoundException {
              if (id != null) {
                  try {
                      Client entity = entityManager.find(Client.class, id);
                      if (entity != null)
                          setId(id);
                  } catch (Exception ex) {
                      log.error("client not found");
                  }
      
              }
          }
      
          @Override
          public Expressions.ValueExpression getCreatedMessage() {
              return createValueExpression("#{messages['client.created']}");
          }
      
          @Override
          public Expressions.ValueExpression getUpdatedMessage() {
              return createValueExpression("#{messages['client.updated']}");
          }
      
          @Override
          public String update() {                       
              return super.update();    
          }
      }
      


      my view



      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:s="http://jboss.com/products/seam/taglib"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:rich="http://richfaces.org/rich"
                      template="layout/template.xhtml">
      
          <ui:define name="body">
              <h:messages id="messages" globalOnly="true" styleClass="message"
                          errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
                          rendered="#{showGlobalMessages != 'false'}"/>
      
              <h1>
                  <h:outputText rendered="#{clientHome.managed}" value="#{messages['client.edit']}"/>
                  <h:outputText rendered="#{!clientHome.managed}" value="#{messages['client.create']}"/>
              </h1>
              <h:form id="editClient">
                  <h:panelGrid columns="2">
                      <h:outputText styleClass="manage-form" value="#{messages['client.surname']}"/>
                      <h:inputText id="surname" value="#{editClient.surname}" size="30"/>
                      <h:outputText styleClass="manage-form" value="#{messages['client.name']}"/>
                      <h:inputText id="name" value="#{editClient.name}" size="30"/>
                      <h:outputText styleClass="manage-form" value="#{messages['client.patronymic']}"/>
                      <h:inputText id="patronymic" value="#{editClient.patronymic}" size="30"/>
                      <h:outputText styleClass="manage-form" value="#{messages['client.email']}"/>
                      <h:inputText id="email" value="#{editClient.email}" size="30"/>
                      <h:outputText styleClass="manage-form" value="#{messages['label.groups']}"/>
                      <h:selectManyCheckbox value="#{editClient.clientGroups}" layout="PAGE_DIRECTION">
                          <s:selectItems value="#{all_groups.resultList}"
                                         var="group"
                                         label="#{group.name}"/>
                          <s:convertEntity/>
                      </h:selectManyCheckbox>
                      <h:outputText styleClass="manage-form" value="#{messages['client.subscribed']}"/>
                      <h:selectBooleanCheckbox value="#{editClient.subscribed}"/>
                  </h:panelGrid>
                  <br/>
                  <rich:separator/>
                  <h:commandButton rendered="#{not clientHome.managed}" value="#{messages['button.save']}"
                                   action="#{clientHome.persist}"/>
                  <h:commandButton rendered="#{clientHome.managed}" value="#{messages['button.update']}"
                                   action="#{clientHome.update}"/>
              </h:form>
      
          </ui:define>
      
      </ui:composition>
      



      When I select some items and press update/create it gave my select items to EntityHome but after persist/update nothing happens in db, for other fields of Client entity everything ok.


      I use JBoss SEAM 2.1 and JBoss AS 4.2.2.


        • 1. Re: EntityHome and h:selectManyCheckbox problem
          v.masterov

          The first thing you have field id in hashCode() method. You should remove it from calculation hashCode because before persist and after persist you will have different hashCode. Calculate hashCode without id-field. Also it is better to remove this field from equals() method.
          I have some more ideas about your trouble but try to make it firstly.
          Maybe it will help.

          • 2. Re: EntityHome and h:selectManyCheckbox problem
            gregtk

            Thank you for reply :)
            I tryed to use your advice, but it's doesn't helped. Than I tryed add hashCode() and equals() to ClietGroup class, but it's doesn't change anything. :( 

            • 3. Re: EntityHome and h:selectManyCheckbox problem
              v.masterov

              You have bidirectional association between Client and ClientGroup.
              Then try to define explicitly method persist() in ClientHome class like this:


              @In(create = true)
              private EntityHome<ClientGroup> clientGroupHome;
              
              @Override
              public String persist() {
                  super.persist();
                  Iterator<ClientGroup> iterGroups = getInstance().getClientGroups().iterator();
                  while (iterGroups.hasNext()) {
                      ClientGroup clientGroup = iterGroups.next();
                      clientGroup.getClients().add(getInstance());
                      clientGroupHome.setInstance(clientGroup);
                      clientGroupHome.update();
                  }
                  return "persisted";
              }
              


              If it helped for you, then you can get the same effect when you will use additional hibernate annotations for mapping association.

              • 4. Re: EntityHome and h:selectManyCheckbox problem
                gregtk

                I found problem that's in wrong CascadeType on relation between Client and ClientGroup. Thank you for your help. :)