jsf - How to save only one item of select many check box? -


when offer create oferta object (offer) saves of items in select many check box (this location per company selected(empresa)), not checked ones:

thank can provide.

                <h:outputlabel value="#{bundle.createofertalabel_empresaidempresa}" for="empresaidempresa" />                                                         <h:selectonemenu id="empresaidempresa"                                   value="#{ofertacontroller.selected.empresaidempresa}"                                   title="#{bundle.createofertatitle_empresaidempresa}"                                  required="true"                                    requiredmessage="#{bundle.createofertarequiredmessage_empresaidempresa}">                     <f:ajax event="valuechange" execute="empresaidempresa" render="ubicacioncollection" />                     <f:selectitems value="#{empresacontroller.itemsavailableselectone}"/>                 </h:selectonemenu>                  <h:outputlabel value="#{bundle.createofertalabel_ubicacioncollection}" for="ubicacioncollection" />                       <h:selectmanycheckbox id="ubicacioncollection"                                       value="#{ubicacionxempresa}"                                       title="#{bundle.createofertatitle_ubicacioncollection}" >                                         <f:converter id="ubicacionconverter" converterid="ubicacionconverter"/>                     <f:selectitems id="ubicacioncollectionitems"                                    value="#{ofertacontroller.selected.empresaidempresa.ubicacioncollection}"                                    var="ubicacionxempresa"                                    itemlabel="#{ubicacionxempresa.barrio}"                                    itemvalue="#{ubicacionxempresa}"/>                 </h:selectmanycheckbox> 

your correct lines should be:

                    <h:outputlabel value="#{bundle.createofertalabel_ubicacioncollection}" for="ubicacioncollection" />                       <h:selectmanycheckbox id="ubicacioncollection"                                       value="#{ofertacontroller.selected.ubicacioncollection}"                                       title="#{bundle.createofertatitle_ubicacioncollection}" >                                         <f:converter id="ubicacionconverter" converterid="ubicacionconverter"/>                     <f:selectitems id="ubicacioncollectionitems"                                    value="#{ofertacontroller.selected.empresaidempresa.ubicacioncollection}"                                    var="ubicacionxempresa"                                    itemlabel="#{ubicacionxempresa.barrio}"                                    itemvalue="#{ubicacionxempresa}"/>                 </h:selectmanycheckbox> 

but know faces show me on creation of offer

exception [eclipselink-4002] (eclipse persistence services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.databaseexception internal exception: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: duplicate entry '32-1' key 'primary' error code: 1062 call: insert oferta_has_ubicacion (ubicacion_idubicacion, oferta_idoferta) values (?, ?) bind => [2 parameters bound] query: datamodifyquery(name="ubicacioncollection" sql="insert oferta_has_ubicacion (ubicacion_idubicacion, oferta_idoferta) values (?, ?)") 

here create method:

   public void create(oferta oferta) {     if (oferta.getubicacioncollection() == null) {         oferta.setubicacioncollection(new arraylist<ubicacion>());     }     if (oferta.getempresacollection() == null) {         oferta.setempresacollection(new arraylist<empresa>());     }     entitymanager em = null;     try {         em = getentitymanager();         em.gettransaction().begin();         empresa empresaidempresa = oferta.getempresaidempresa();         if (empresaidempresa != null) {             empresaidempresa = em.getreference(empresaidempresa.getclass(), empresaidempresa.getidempresa());             oferta.setempresaidempresa(empresaidempresa);         }         collection<ubicacion> attachedubicacioncollection = new arraylist<ubicacion>();         (ubicacion ubicacioncollectionubicaciontoattach : oferta.getubicacioncollection()) {             ubicacioncollectionubicaciontoattach = em.getreference(ubicacioncollectionubicaciontoattach.getclass(), ubicacioncollectionubicaciontoattach.getidubicacion());             attachedubicacioncollection.add(ubicacioncollectionubicaciontoattach);         }         oferta.setubicacioncollection(attachedubicacioncollection);         collection<empresa> attachedempresacollection = new arraylist<empresa>();         (empresa empresacollectionempresatoattach : oferta.getempresacollection()) {             empresacollectionempresatoattach = em.getreference(empresacollectionempresatoattach.getclass(), empresacollectionempresatoattach.getidempresa());             attachedempresacollection.add(empresacollectionempresatoattach);         }         oferta.setempresacollection(attachedempresacollection);         em.persist(oferta);         if (empresaidempresa != null) {             empresaidempresa.getofertacollection().add(oferta);             empresaidempresa = em.merge(empresaidempresa);         }         (ubicacion ubicacioncollectionubicacion : oferta.getubicacioncollection()) {             ubicacioncollectionubicacion.getofertacollection().add(oferta);             ubicacioncollectionubicacion = em.merge(ubicacioncollectionubicacion);         }         (empresa empresacollectionempresa : oferta.getempresacollection()) {             empresacollectionempresa.getofertacollection().add(oferta);             empresacollectionempresa = em.merge(empresacollectionempresa);         }         em.gettransaction().commit();     } {         if (em != null) {             em.close();         }     } } 

          value="#{ofertacontroller.selected.ubicacioncollection}" 

the other many many relationship problem jpa issue


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -