1 Reply Latest reply on Jul 2, 2013 10:19 AM by morellik

    uploadFile and Derby duplicate key value

    morellik

      Dear,

       

      I receive The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index  when I try to upload a second file using fileUpload to a Derby DB (the first upload ends fine).

       

      The jsf upload page:

       

       

      <h:form>  
                              <h:panelGrid columns="3" columnClasses="top,top">  
                                  <h:panelGroup id="users" layout="block">
                                      <rich:panel bodyClass="info">
                                          <f:facet name="header">
                                              <h:outputText value="Select user to share data" />
                                          </f:facet>
                                          <h:selectOneMenu id="userID" value="#{fileUploadBean.shareUserEmail}" required="true" >
                                              <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
                                              <f:selectItems value="#{fileUploadBean.admin.children}"  var="f" itemValue="#{f.email.toString()}" itemLabel="#{f.firstName} #{f.lastName}" />
                                              <a4j:ajax  listener="#{fileUploadBean.listener(event)}"  /> <!-- Otherwise returns a null value -->
                                          </h:selectOneMenu>
      
                                      </rich:panel>
                                  </h:panelGroup>
                                  <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"  
                                                   id="upload" acceptedTypes="txt,csv,pdf,iso,tar,zip,tgz,gz"  
                                                   ontyperejected="alert('Only Text and CSV files are accepted');"
                                                   doneLabel="#{fileUploadBean.uploadDoneLabel}"
                                                   maxFilesQuantity="5">  
                                      <a4j:ajax event="uploadcomplete" execute="@none" render="info" />  
                                  </rich:fileUpload>  
      
                                  <h:panelGroup id="info" layout="block">
                                      <rich:panel bodyClass="info">
                                          <f:facet name="header">
                                              <h:outputText value="Uploaded Files Info" />
                                          </f:facet>
                                          <h:outputText value="No files currently uploaded" rendered="#{fileUploadBean.size==0}" />
                                          <rich:dataGrid columns="1" value="#{fileUploadBean.files}" var="file" rowKeyVar="row">
      
                                              <rich:panel bodyClass="rich-laguna-panel-no-header">  
                                                  <h:panelGrid columns="2">  
      
                                                      <h:panelGrid columns="2">  
                                                          <h:outputText value="File Name:" />  
                                                          <h:outputText value="#{file.name}" />  
                                                          <h:outputText value="File Length(bytes):" />  
                                                          <h:outputText value="#{file.length}" />  
      
                                                      </h:panelGrid>
                                                  </h:panelGrid>
                                              </rich:panel>
                                          </rich:dataGrid>
                                      </rich:panel>
                                      <a4j:commandButton action="#{fileUploadBean.clearUploadData}"  
                                                         render="info" value="Clear Info Data"  
                                                         rendered="#{fileUploadBean.size>0}" />  
      
                                  </h:panelGroup>
                              </h:panelGrid>
                              <h:inputHidden value="#{fileUploadBean.uploader}" />
                          </h:form> 
      

       

       

      The Bean:

       

       

      public void listener(FileUploadEvent event) throws Exception {
              UploadedFile item = event.getUploadedFile();
              UploadedText file = new UploadedText();
              file.setLength(item.getData().length);
              file.setName(item.getName());
              //file.setData(item.getData());
              files.add(file);
              String filename = fPath + item.getName();
              FileOutputStream fop = null;
              File fout;
              if (file.getLength() > 0) {
                  try {
                      fout = new File(filename);
                      fop = new FileOutputStream(fout);
      
                      if (!fout.exists()) {
                          fout.createNewFile();
                      }
                      fop.write(item.getData());
                      fop.flush();
                      fop.close();
                      byte[] md5 = MD5Checksum.createChecksum(filename);
                      ufile.setDirectory(fPath);
                      ufile.setFilename(filename);
                      ufile.setFilesize(file.getLength());
                      ufile.setMd5sum(md5.toString());
                      Date date = new Date();
                      ufile.setUpload_date(date);
                      ufile = uploadFileDAO.create(ufile);
                      System.out.println(shareUserEmail + " " + uploader + " " + ufile.getId());
                      localuserDAO.addFile(shareUserEmail, ufile.getId());
                      uploadFileDAO.addUploader(uploader, ufile.getId());
                      uploadDoneLabel = "File Uploaded Successfully";
                  } catch (IOException e) {
      
                  } finally {
                      try {
                          if (fop != null) {
                              fop.close();
                          }
                      } catch (IOException e) {
      
                      }
                  }
      
              }
      

       

      and the Entity:

       

       

      @Entity
      public class UploadFile implements Serializable {
      
          private static final long serialVersionUID = 1L;
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Long id;
          @Column(nullable = false, length = 128)
          private String directory;
          @Column(nullable = false, length = 128)
          private String filename;
          @Temporal(TemporalType.DATE)
          private Date upload_date;
          private Long filesize;
          @Column(nullable = true, length = 34)
          private String md5sum;
          @Column(nullable = true)
          private Boolean download;
          @Temporal(TemporalType.DATE)
          private Date download_date;
          @ManyToMany(fetch = FetchType.EAGER)
          @JoinTable(name = "LocalUser_File",
                  joinColumns =
                  @JoinColumn(name = "LOCALUSER_EMAIL"),
                  inverseJoinColumns =
                  @JoinColumn(name = "FILE_ID"))
          private List<LocalUser> localuser;
          private String uploaded_by;
          @ManyToOne
          private LocalUser uploader;
      

       

       

      Where I'm wrong?

       

      Thanks