2014-02-05 177 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問題