0 Replies Latest reply on Mar 18, 2011 9:45 AM by aniinprni

    Cannot delete or update a parent row: a foreign key constraint fails

    aniinprni Newbie
      18:58:10,816 WARN  [PermissionManager] no permission store available - please install a PermissionStore with the name 'org.jboss.seam.security.jpaPermissionStore' if permission management is required.
      18:58:24,644 INFO  [STDOUT] in the lajflasjflasfjlasdfjalsfjasldfjlasdfj
      18:58:24,644 INFO  [STDOUT] kksafjsldfjsalfjaslfjaslfjalsdfjasdf8
      18:58:28,410 WARN  [JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000
      18:58:28,410 ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`secadmin/adm_payments`, CONSTRAINT `FK6EB9E72274381B2F` FOREIGN KEY (`parent_payment_id`) REFERENCES `adm_payments` (`payment_id`))
      18:58:28,410 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update



      this is my action class===
      ==========================================================
      @Name("paymentsAction")
      @Scope(ScopeType.CONVERSATION)
      public class PaymentsAction {
             
              private AdmPayments payments = new AdmPayments();
             
              private AdmPaymentChequeInfo cheque= new AdmPaymentChequeInfo();;
             
              private AdmPaymentDDInfo dd=new AdmPaymentDDInfo();
              private int recordsPerPage = 5;

              @In
              EntityManager entityManager;

              @In(required = true, scope = ScopeType.SESSION)
              User user;

              @In(required = false, scope = ScopeType.PAGE)
              @Out(required = false, scope = ScopeType.PAGE)
              private List<AdmPayments> paymentsHistory;

              @In(required = false, scope = ScopeType.SESSION)
              @Out(required = false, scope = ScopeType.SESSION)
              AdmPayments paymnt;
             
             
              @In(required = false)
              DeleteAction deleteManager;

              private Date paidDate;
              private Date dateFrom;
              private Date dateTo;

              Date date = new Date();

             
              //confirm method
              public String confirmPayments() {
                      if (validate()) {
                              if (payments.getId() == 0) {
                                      payments.setCreateDate(date);
                                      payments.setCreateUser(this.user.getUsername());
                                      if(payments.getPayMode()==AdmPayments.PayMode.Cash)
                                      {
                                              payments.setChequeInfo(null);
                                              payments.setDdInfo(null);
                                              entityManager.persist(payments);
                                              entityManager.refresh(payments);
                                      }
                                      else if (payments.getPayMode()== AdmPayments.PayMode.Cheque)
                                      {
                                              entityManager.persist(payments);
                                              cheque.setPaymentInfo(payments);
                                              entityManager.persist(cheque);
                                              entityManager.refresh(cheque);
                                              payments.setChequeInfo(cheque);
                                              payments.setDdInfo(null);
                                              entityManager.merge(payments);
                                              entityManager.refresh(payments);
                                      }
                                      else if(payments.getPayMode()== AdmPayments.PayMode.DD)
                                      {
                                              entityManager.persist(payments);
                                              dd.setPaymentDDInfo(payments);
                                              entityManager.persist(dd);
                                              payments.setDdInfo(dd);
                                              payments.setChequeInfo(null);
                                              entityManager.merge(payments);
                                      }
                                     

                              } else {
                                      payments.setModifyDate(date);
                                      payments.setModifyUser(this.user.getUsername());
                                      entityManager.merge(payments);
                              }
                              AdmPayments paymentsHistory = payments.clone();
                              paymentsHistory.setParentPaymentId(payments);
                              paymentsHistory.setHistory(true);
                              entityManager.persist(paymentsHistory);
                              entityManager.refresh(payments);
                              if (paymentsHistory.getPayMode()== AdmPayments.PayMode.Cheque)
                              {
                                      System.out.println("in the lajflasjflasfjlasdfjalsfjasldfjlasdfj");
                                      AdmPaymentChequeInfo ce=this.cheque.clone();
                                      AdmPayments histo=paymentsHistory;
                                      System.out.println("kksafjsldfjsalfjaslfjaslfjalsdfjasdf"+histo.getId());
                                     
                                      ce.setPaymentInfo(histo);
                                      entityManager.merge(ce);
                                      entityManager.refresh(cheque);
                              }
                              else if(paymentsHistory.getPayMode()== AdmPayments.PayMode.DD)
                              {
                                      AdmPaymentDDInfo dd= new AdmPaymentDDInfo();
                                      dd=paymentsHistory.getDdInfo();
                                      dd.setPaymentDDInfo(paymentsHistory);
                                      entityManager.persist(dd);
                              }
                              entityManager.flush();
                              Conversation.instance().end();
                              return "confirm-payments";
                      } else
                              return " ";

              }
           //validate method
              public boolean validate() {
                      boolean result = true;
                      java.util.ResourceBundle resourceBundle = ResourceBundle.instance();
                              if (this.payments.getPaymentFor() != null && this.payments.getPaymentFor().trim().length() == 0) {
                                      String message = resourceBundle.getString("validationMessage.onlyWhiteSpaces");
                                      validationMessage("paymentsform", "paymentsForId", "paymentfor",message);
                                      result = false;
                              }
                              if (this.payments.getPaidBy() != null && this.payments.getPaidBy().trim().length() == 0) {
                                      String message = resourceBundle.getString("validationMessage.onlyWhiteSpaces");
                                      validationMessage("paymentsform", "paidById", "padiBy", message);
                                      result = false;
                              }
                              if (this.payments.getAuthorizedBy() != null && this.payments.getAuthorizedBy().trim().length() == 0) {
                                      String message = resourceBundle.getString("validationMessage.onlyWhiteSpaces");
                                      validationMessage("paymentsform", "authorizedById", "authorizedBy",message);
                                      result = false;
                              }
                              if (this.payments.getAmount() < 0.0) {
                                      String message = resourceBundle.getString("validationMessage.amount");
                                      validationMessage("paymentsform", "amountId", "amount", message);
                                      result = false;
                              }
                              if (this.payments.getVoucherNo() != null && this.payments.getVoucherNo().trim().length() == 0) {
                                      String message = resourceBundle.getString("validationMessage.onlyWhiteSpaces");
                                      validationMessage("paymentsform", "voucherId", "voucher", message);
                                      result = false;
                              }
                      return result;
              }

              public void validationMessage(String formId, String decorateId,
                              String fieldId, String errorMessage) {
                      FacesMessage message = new FacesMessage();
                      message.setSeverity(FacesMessage.SEVERITY_ERROR);
                      message.setSummary(errorMessage);
                      message.setDetail(errorMessage);
                      FacesContext.getCurrentInstance().addMessage(formId + ":" + decorateId + ":" + fieldId, message);
              }
        // retrieve method
              @Begin(join = true)
              public List<AdmPayments> retrievePayments() {
                      if (payments != null) {
                              if (payments.getDateFilter() != DateFilters.DateRange) {
                                      this.setDateFrom(null);
                                      this.setDateTo(null);
                              }
                              if (payments.getDateFilter() != DateFilters.Date) {
                                      this.setPaidDate(null);
                              }
                      }

                      StringBuffer query = new StringBuffer("from AdmPayments adm where 1 = 1 and adm.history=0 ");
                      if (payments.getDateFilter() != null) {
                              if (payments.getDateFilter().equals(AdmPayments.DateFilters.Date) && this.getPaidDate() != null) {
                                      query.append("and adm.paidDate = :paidDate ");
                              }
                              if (payments.getDateFilter().equals(AdmPayments.DateFilters.DateRange) && this.getDateFrom() != null && this.getDateTo() != null) {
                                      query.append("and adm.paidDate >= :dateFrom ");
                                      query.append("and adm.paidDate <= :dateTo ");
                              }
                      }
                     
                      Query q = this.entityManager.createQuery(query.toString());

                      if (payments.getDateFilter() != null) {
                              if (payments.getDateFilter().equals(AdmPayments.DateFilters.Date)
                                              && this.getPaidDate() != null) {
                                      q.setParameter("paidDate", this.getPaidDate());
                              }
                              if (payments.getDateFilter().equals(
                                              AdmPayments.DateFilters.DateRange)
                                              && this.getDateFrom() != null && this.getDateTo() != null) {
                                      q.setParameter("dateFrom", this.getDateFrom());
                                      q.setParameter("dateTo", this.getDateTo());
                              }

                      }
                      List<AdmPayments> list = q.getResultList();
                      return list;
              }

              //add method
              @Begin(join = true)
              public String addPayments() {
                      this.payments = new AdmPayments();
                      this.cheque= new AdmPaymentChequeInfo();
                      this.dd =new AdmPaymentDDInfo();
                      return "edit-payments-detail";
              }

              public void flush() {
                      entityManager.flush();
              }

              public AdmPayments getPayments() {
                      return payments;
              }

              public void setPayments(AdmPayments payments) {
                      this.payments = payments;
              }

              // Edit Employee Method

              @Begin(join = true)
              public String editPaymentsDetails(AdmPayments payment) {
                      this.payments = payment;
                      return "edit-payments-detail";
              }

              public List<AdmPayments> historyOfPayments(AdmPayments paymnt) {

                      this.paymentsHistory = this.retrievepaymentsHistory(paymnt);
                      return this.paymentsHistory;
              }

              public List<AdmPayments> retrievepaymentsHistory(AdmPayments payments) {
                      return entityManager.createQuery("from AdmPayments fc where  fc.history= 1 and fc.parentPaymentId= :id order by fc.modifyDate desc")
                                 .setParameter("id", payments).getResultList();
              }
             
              //Details method
             
              @Begin(join = true)
              public String getPaymentDeetails(AdmPayments payments) {
                      this.paymnt = new AdmPayments();
                      this.paymnt = payments;
                      return "payments-details";
              }

              //Payment search method
              @Begin(join = true)
              public void seachPaymentDetails() {
                      if(validateDate()) {
                              if(payments.getDateFilter() != null) {
                                if(payments.getDateFilter().equals(AdmPayments.DateFilters.DateRange)) {
                                              if(this.getDateFrom() != null && this.getDateTo() != null) {
                                                              this.setDateFrom(new SystemUtils(entityManager).getServerRoundedDownDate(this.getDateFrom()));
                                                              this.setDateTo(new SystemUtils(entityManager).getServerRoundedUpDate(this.getDateTo()));
                                                }
                                      }
                                      if (payments.getDateFilter().equals(AdmPayments.DateFilters.Date)) {
                                               this.setPaidDate(this.getPaidDate());
                                      }
                              }
                      }
              }

              public boolean validateDate() {
                      if (this.getDateFrom() != null && this.getDateTo() != null) {
                              if (this.getDateFrom().after(this.getDateTo())) {
                                      String msg = ResourceBundle.instance().getString("validationMessage.toDateGreaterThanFromDate");
                                      validationMessage("paymentFormId", "dateTo", "resToDateId", msg);
                                      return false;
                              }
                      }
                      return true;

              }

             
      //For delete
              public void refresh(AdmPayments payments) {
                      AdmPayments pays=entityManager.find(AdmPayments.class, payments.getId());
                      entityManager.refresh(pays);
                      AdmPaymentChequeInfo cheques=entityManager.find(AdmPaymentChequeInfo.class, payments.getChequeInfo().getId());
                      entityManager.refresh(cheques);
                      deleteManager.setDeleteObject(pays);
                     
              }

             
             
              public String cancel() {
                      return "confirm-payments";
              }

              public int getRecordsPerPage() {
                      return recordsPerPage;
              }

              public void setRecordsPerPage(int recordsPerPage) {
                      this.recordsPerPage = recordsPerPage;
              }

              public List<AdmPayments> getPaymentsHistory() {
                      return paymentsHistory;
              }

              public void setPaymentsHistory(List<AdmPayments> paymentsHistory) {
                      this.paymentsHistory = paymentsHistory;
              }

              public AdmPayments getPaymnt() {
                      return paymnt;
              }

              public void setPaymnt(AdmPayments paymnt) {
                      this.paymnt = paymnt;
              }

             
              public Date getPaidDate() {
                      return paidDate;
              }

              public void setPaidDate(Date paidDate) {
                      this.paidDate = paidDate;
              }

              public Date getDateFrom() {
                      return dateFrom;
              }

              public void setDateFrom(Date dateFrom) {
                      this.dateFrom = dateFrom;
              }

              public Date getDateTo() {
                      return dateTo;
              }

              public void setDateTo(Date dateTo) {
                      this.dateTo = dateTo;
              }
              public String backToPaymentPage(){
                      return "confirm-payments";
              }
              public AdmPaymentChequeInfo getCheque() {
                      return cheque;
              }
              public void setCheque(AdmPaymentChequeInfo cheque) {
                      this.cheque = cheque;
              }
              public AdmPaymentDDInfo getDd() {
                      return dd;
              }
              public void setDd(AdmPaymentDDInfo dd) {
                      this.dd = dd;
              }
      }


      here i am creating one record,and then immediately click on delete it is showing above error and again if i click on delete button it is deleting the record .

      please help me to rectify this error

      thank u