org.springframework.dao.InvalidDataAccessApiUsageException
Asked Answered
G

2

8

I am using Spring MVC + Hibernate

Generic Method

public <T, E> void saveOrUpdate(T entity, List<E> list) throws DataAccessException {

    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(entity);
    for(E getlist : list){
    session.saveOrUpdate(getlist);
    }
}

(Following )Controller Working Fine

@Controller
public class FinGeneralJournalController {

    protected static Logger log = Logger
        .getLogger(FinGeneralJournalController.class);

    @Resource(name = "PersistenceTemplate")
    private PersistenceTemplate pt;

    @RequestMapping(value = "/AddFinGeneralJournal", method = RequestMethod.POST)
    public @ResponseBody
    JsonResponseStatus addGeneralJournalEntry(
        @RequestParam(value="description", required=true) String description,
        @RequestParam(value="trxDate", required=true) Date trxDate,
        @RequestParam(value="sourceDocumentId", required=true) long sourceDocumentId,
        @RequestParam(value="currencyId", required=true) long currencyId,
        @RequestParam(value="batchId", required=true) long batchId,
        @RequestParam(value="tableList", required=true) String tableList, HttpSession session ) {

        Date sysdate = null;
        JsonResponseStatus response = null;
        List<FinGeneralJournalEntryModel> list=null;
        FinGeneralJournalEntryModel generalJournalEntryModel= null;
        try {
            long userId=Long.parseLong(session.getAttribute("userId").toString());
            String userIp=session.getAttribute("userIp").toString();

            sysdate = new Date();
            response = new JsonResponseStatus();

            FinGeneralJournalModel generalJournalModel  = new FinGeneralJournalModel(description,
                trxDate, userId, userIp, sysdate, 0);

            generalJournalModel.setFko_gj_batch(new FinBatchModel(batchId));
            generalJournalModel.setFko_gj_srcdoc(new FinSourceDocumentModel(sourceDocumentId));
            generalJournalModel.setFko_gj_curr(new GenCurrencyModel(currencyId));

            list= new ArrayList<FinGeneralJournalEntryModel>();

            String []getDistributionlist=tableList.split(",");

            for (int i=0; i<getDistributionlist.length; i+=6){

                generalJournalEntryModel= new FinGeneralJournalEntryModel(Long.parseLong(getDistributionlist[i+3].toString()),
                        Long.parseLong(getDistributionlist[i+4].toString()),
                        currencyId, 86.5, userId, userIp, sysdate, 0);

                generalJournalEntryModel.setFko_gje_gj(generalJournalModel);
                generalJournalEntryModel.setFko_gje_coam(new FinCOAMaintenanceModel(Long.parseLong(getDistributionlist[i].toString())));

                list.add(generalJournalEntryModel);
            }

            pt.saveOrUpdate(generalJournalModel,list);

            response.setStatus("Success");
        } catch (Exception ex) {
            log.error("Exception.." + ex);
            response.setStatus("Fail");
        }
        return response;
    }
}

another following controller same like above but giving exception

@Controller
public class ProPurchaseOrderController {
    protected static Logger log = Logger
        .getLogger(ProPurchaseOrderController.class);

    @Resource(name = "PersistenceTemplate")
    private PersistenceTemplate pt;

    @RequestMapping(value = "/AddProPurchaseOrder", method = RequestMethod.POST)
    public @ResponseBody
    JsonResponseStatus AddProPurchaseOrder(
        @RequestParam(value="poDesc", required=true) String poDesc,
        @RequestParam(value="poTrxDate", required=true) Date poTrxDate,
        @RequestParam(value="supplierId", required=true) long supplierId,
        @RequestParam(value="currencyId", required=true) long currencyId,
        @RequestParam(value="batchId", required=true) long batchId,
        @RequestParam(value="poSubTotal", required=true) double poSubTotal,
        @RequestParam(value="poDiscount", required=true) double poDiscount,
        @RequestParam(value="poFreight", required=true) double poFreight,
        @RequestParam(value="poTax", required=true) double poTax,
        @RequestParam(value="poMisc", required=true) double poMisc,
        @RequestParam(value="poGTotal", required=true) double poGTotal,
        @RequestParam(value="tableList", required=true) String tableList, HttpSession session ) {

        Date sysdate = null;
        JsonResponseStatus response = null;
        ProPurchaseOrderModel purchaseOrder=null;
        ProPurchaseOrderEntryModel purchaseOrderEntry=null;
        List<ProPurchaseOrderEntryModel> list=null;

        try {
            sysdate = new Date();
            response = new JsonResponseStatus();

            long userId=Long.parseLong(session.getAttribute("userId").toString());
            String userIp=session.getAttribute("userIp").toString();

            purchaseOrder= new ProPurchaseOrderModel(poDesc, poTrxDate, poSubTotal, poFreight, poTax, poMisc, poDiscount,
                0, 0, userId, userIp, sysdate, 0);

            purchaseOrder.setFko_po_batch(new ProBatchModel(batchId));
            purchaseOrder.setFko_po_currency(new GenCurrencyModel(currencyId));
            purchaseOrder.setFko_po_supp(new ProSupplierModel(supplierId));

            list= new ArrayList<ProPurchaseOrderEntryModel>();

            String []getItemlist=tableList.split(",");

            for (int i=0; i<getItemlist.length; i+=11){

                double actualQuantity=Double.parseDouble(getItemlist[i+5].toString());
                double fraction=Double.parseDouble(getItemlist[i+6].toString());
                double baseQuantity=Double.parseDouble(getItemlist[i+7].toString());
                double cost=Double.parseDouble(getItemlist[i+8].toString());
                double total=Double.parseDouble(getItemlist[i+9].toString());

                purchaseOrderEntry= new ProPurchaseOrderEntryModel(actualQuantity, fraction, baseQuantity, cost, total,
                    0, 0, userId, userIp, sysdate, 0);

                purchaseOrderEntry.setFko_poe_uome(new InvUOMExtendedModel(Long.parseLong(getItemlist[i+4].toString())));
                purchaseOrderEntry.setFko_poe_item(new InvItemModel(Long.parseLong(getItemlist[i].toString())));
                purchaseOrderEntry.setFko_poe_po(purchaseOrder);

                list.add(purchaseOrderEntry);
            }

            pt.saveOrUpdate(purchaseOrderEntry,list);
            response.setStatus("Success");
        } catch (Exception ex) {
            log.error("Exception.." + ex);
            response.setStatus("Fail");
        }
        return response;
    }
}

Exception is

Exception..org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.soft.erp.pro.model.ProPurchaseOrderModel; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

Garnetgarnett answered 27/7, 2013 at 19:59 Comment(0)
T
3

As the error message states: your ProPurchaseOrderModel has a reference to another entity (most probably InvItemModel and/or InvUOMExtendedModel) which itself is not saved yet. Either you save these entities first, or (probably better) you fix your mapping so that these referenced objects will be saved automatically by declaring a cascade for persist.

If you are using annotations, this done for example on a many-to-one relationship with

    @ManyToOne(cascade = CascadeType.PERSIST)

or even

    @ManyToOne(cascade = CascadeType.ALL)
Thorncombe answered 27/7, 2013 at 21:1 Comment(0)
G
0

Regardless of the code if you ever had as a nested exception org.springframework.dao.InvalidDataAccessApiUsageException with a docker DB and it was working fine before without any new changes consider restarting the computer, we lost 2 hours trying to debug this / invalidate cache/ restarting docker and it just worked after restarting the whole machine (MAC M1)

Getaway answered 27/7, 2023 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.