2014-02-05 1 views
0

когда предложение создать объект OFERTA (предложение) он сохраняет все элементы в выберите много флажка (это есть место, как в компанию выбрана (Empresa)), а не только проверенные из них:Как сохранить только один элемент для выбора многих флажков?

Спасибо очень много для любой помощи, которую вы можете предоставить.

   <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> 

Ваши правильные линии должны быть:

    <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> 

Но знаете лица показать мне это на создании предложения

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' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?) bind => [2 parameters bound] Query: DataModifyQuery(name="ubicacionCollection" sql="INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?)") 

Вот мой создать метод:

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>(); 
     for (Ubicacion ubicacionCollectionUbicacionToAttach : oferta.getUbicacionCollection()) { 
      ubicacionCollectionUbicacionToAttach = em.getReference(ubicacionCollectionUbicacionToAttach.getClass(), ubicacionCollectionUbicacionToAttach.getIdUbicacion()); 
      attachedUbicacionCollection.add(ubicacionCollectionUbicacionToAttach); 
     } 
     oferta.setUbicacionCollection(attachedUbicacionCollection); 
     Collection<Empresa> attachedEmpresaCollection = new ArrayList<Empresa>(); 
     for (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); 
     } 
     for (Ubicacion ubicacionCollectionUbicacion : oferta.getUbicacionCollection()) { 
      ubicacionCollectionUbicacion.getOfertaCollection().add(oferta); 
      ubicacionCollectionUbicacion = em.merge(ubicacionCollectionUbicacion); 
     } 
     for (Empresa empresaCollectionEmpresa : oferta.getEmpresaCollection()) { 
      empresaCollectionEmpresa.getOfertaCollection().add(oferta); 
      empresaCollectionEmpresa = em.merge(empresaCollectionEmpresa); 
     } 
     em.getTransaction().commit(); 
    } finally { 
     if (em != null) { 
      em.close(); 
     } 
    } 
} 
+0

Просьба показать, как вы собираете выбранные значения. Значение ' не имеет для меня никакого смысла. – BalusC

+0

@BalusC обновленный вопрос, спасибо BalusC –

ответ

0
  value="#{ofertaController.selected.ubicacionCollection}" 

Другая проблема была связана со многими и многими проблемами jpa issue

Смежные вопросы