'object references an unsaved transient instance - save the transient instance before flush?

There are two entity in my project: 1)

public class DonationRegisterMaster extends CommonColumn {
 

       @Column(name = "transaction_no", nullable = false, unique = true)
        private String transactionNo;
    
        @Column(name = "transaction_date", nullable = false)
        private Date transactionDate;
    
        @Column(name = "donorCode", nullable = false)
        private String donorCode;
    
        @Column(name = "program_type")
        private String programType;
    
        @Column(name = "currency")
        private String currency;
    
        @Column(name = "currency_amount")
        private double currencyAmount = 0.0;
    
        @Column(name = "terminal")
        private String terminal;
    
        @Column(name = "receive_type")
        private String receiveType;
    
        @Column(name = "receiving_bank_account")
        private String receivingBankAccount;
    
        @Column(name = "status")
        private String status;
    
        @Column(name = "attachment_file_path")
        private String attachmentFilePath;
    
        @Column(name = "feature_name")
        private String featureName;
    
        @Column(name = "is_submitted", columnDefinition = "boolean default false")
        private boolean isSubmitted;
    
        @Column(name = "particulars")
        private String particulars;
    
        @Column(name="amount")
        private Double amount = 0.0;
    
        @Column(name="rate")
        private Double rate = 0.0;
    
    //    @JsonManagedReference
      //  @Transient
    @OneToMany(mappedBy = "donationRegisterMaster", fetch = FetchType.LAZY)
     //   @OneToMany(mappedBy = "donationRegisterMaster", orphanRemoval = true, cascade = {CascadeType.ALL})
        List<DonationRegisterDetails> donationRegisterDetailsList;
    }
  1. another entity is:

    public class DonationRegisterDetails extends CommonColumn {

    // @JsonBackReference @ManyToOne(fetch = FetchType.LAZY) // @ManyToOne() @JoinColumn(name = "donation_register_master_id", nullable = false) private DonationRegisterMaster donationRegisterMaster;

     @Column(name = "sl_no")
     private Integer slNo;
    
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "chart_of_accounts_id", nullable = false)
     private ChartOfAccounts chartOfAccounts;
    
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "project_setup_id", nullable = false)
     private ProjectSetup projectSetup;
    
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "area_current_account_code_id")
     private AreaCurrentAccountCode areaCurrentAccountCode;
    
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "subsidiary_ledger_id")
     private SubsidiaryLedger subsidiaryLedger;
    
     @Column(name = "amount")
     private Double amount = 0.0;
    
     @Column(name = "fc_amount")
     private Double fcAmount = 0.0;
    
     @Column(name = "remarks")
     private String remarks;
    

    // @Transient @Column(columnDefinition = "boolean default false") private boolean isDeleted; }

I have faced this error:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bracits.financeho.entity.DonationRegisterDetails.donationRegisterMaster -> com.bracits.financeho.entity.DonationRegisterMaster; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bracits.financeho.entity.DonationRegisterDetails.donationRegisterMaster.

My JavaCode is:

    private DonationRegisterMasterModel updateDonationRegister(DonationRegisterMaster donationRegisterMaster) throws ServiceException {
        DonationRegisterMasterModel donationRegisterMasterModel = new DonationRegisterMasterModel();
        List<DonationRegisterDetails> totalDonationDetails = new ArrayList<>();
        DonationRegisterMasterModel getDonationRegisterMasterModels = new DonationRegisterMasterModel(donationRegisterMaster);
        DonationRegisterMasterModel findDonationRegisterMasterModels = new DonationRegisterMasterModel(this.findById(getDonationRegisterMasterModels.getId()));

        try {
            for (DonationRegisterDetails donationRegisterDetails : donationRegisterMaster.getDonationRegisterDetailsList()) {

                if (donationRegisterDetails.getId() == null) {
                    donationRegisterDetails.setDonationRegisterMaster(donationRegisterMasterService.findById(donationRegisterMaster.getId()));
                    donationRegisterDetailsService.create(donationRegisterDetails);
                    totalDonationDetails.add(donationRegisterDetails);
                } else {

                    if (donationRegisterDetails.isDeleted()) {
                        // Delete & Create
                        donationRegisterDetailsService.delete(donationRegisterDetails.getId());
                    } else {
                        // Update & Create
                        donationRegisterDetails.setDonationRegisterMaster(new DonationRegisterMaster(findDonationRegisterMasterModels));
                        donationRegisterDetails.getDonationRegisterMaster();
                        donationRegisterDetailsService.update(donationRegisterDetails.getId(), donationRegisterDetails);
                        totalDonationDetails.add(donationRegisterDetails);
                    }
                }
            }

            if (donationRegisterMaster.getId() == null) {
//                donationRegisterMaster = this.create(donationRegisterMaster);
                throw new RuntimeException("Donation Master is not found.");
            } else {
                donationRegisterMaster.setDonationRegisterDetailsList(totalDonationDetails);
                donationRegisterMaster = this.update(donationRegisterMaster.getId(), donationRegisterMaster);
            }

            donationRegisterMasterModel = new DonationRegisterMasterModel(donationRegisterMaster);

        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }

        return donationRegisterMasterModel;
    }

I have tried Cascade.All in entity also. But it is not working. What I have to do solve this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source