如何修复:当我从数据库中获取数据时,JPA调用更新?

时间:2016-03-02 01:59:42

标签: java oracle hibernate jpa

我使用JPA从数据库获取数据,我的代码:

public OrderDetails findAllOrders(int page, int limit, int merchantId, String updated_from, String updated_to,
        String created_from, String created_to, String state) {
    OrderDetails msg = null;
    List<OrderResponse> lstOrders = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        em.getTransaction().begin();
        StoredProcedureQuery sp = em.createStoredProcedureQuery("UORDER.GetAllOrders", OrderResponse.class);
        sp.registerStoredProcedureParameter("i_page", Integer.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_limit", Integer.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_merchant_id", Integer.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("o_total_record", Integer.class, ParameterMode.OUT);
        sp.registerStoredProcedureParameter("o_cur_order", Void.class, ParameterMode.REF_CURSOR);
        sp.registerStoredProcedureParameter("i_updated_from", String.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_updated_to", String.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_created_from", String.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_created_to", String.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("i_state", String.class, ParameterMode.IN);
        sp.registerStoredProcedureParameter("o_data", String.class, ParameterMode.OUT);
        sp.registerStoredProcedureParameter("o_code", Integer.class, ParameterMode.OUT);
        sp.setParameter("i_page", page);
        sp.setParameter("i_limit", limit);
        sp.setParameter("i_merchant_id", merchantId);
        sp.setParameter("i_updated_from", updated_from);
        sp.setParameter("i_updated_to", updated_to);
        sp.setParameter("i_created_from", created_from);
        sp.setParameter("i_created_to", created_to);
        sp.setParameter("i_state", state);
        sp.execute();

        lstOrders = new ArrayList<>();
        int count = (int) sp.getOutputParameterValue("o_total_record");
        int code = (int) sp.getOutputParameterValue("o_code");
        String data = (String) sp.getOutputParameterValue("o_data");

        if (code == Constant.SUCCESS) {
            lstOrders = sp.getResultList();
//              em.close();
             for (OrderResponse orders : lstOrders) {
                orders.setItems(getItemByOrder(orders.getId()));
                orders.setBill_address(getBillAddressByOrder(orders.getId()));
                orders.setShip_address(getShipAddressByOrder(orders.getId()));
                orders.setLoyalty(getLoyaltyByOrder(orders.getId()));
                orders.setPromotion(getPromotionByOrder(orders.getId()));
                orders.setAdjustments(getAdjustmentByOrder(orders.getId()));
                orders.setPayments(getPaymentByOrder(orders.getId()));
                if (orders.getShip_address() != null) {
                    orders.setShipments(getShipmentByOrder(orders.getId(), orders.getShip_address().getId()));
                }
            }
            msg = new OrderDetails();
            msg.setItems(lstOrders);
            msg.setCount(count);
        } else if (code == Constant.BAD_REQUEST) {
            em.close();
            throw new NotFoundException();
        } else {
            em.close();
            throw new EntityNotFoundException(data);
        }
        em.getTransaction().commit();
    } catch (Exception e) {
        e.getMessage();
    }finally {
        em.close();
    }
    return msg;
}

它仍然是响应数据,但控制台出错:

  

错误代码:904调用:UPDATE ORDERS SET BILL_ADDRESS_ID = ?,   SHIP_ADDRESS_ID =?,LOYALTY_ID =?,PROMOTION_ID =? WHERE(ID =?)     bind =&gt; [5个参数绑定]查询:   UpdateObjectQuery(com.sunrise.checkout.message.OrderResponse@656c8449)

你能告诉我为什么以及如何解决这个问题? 谢谢你的帮助。

我的存储过程代码:

PROCEDURE GetAllOrders(i_page         NUMBER,
                     i_limit        NUMBER,
                     i_merchant_id  NUMBER,
                     o_total_record OUT NUMBER,
                     o_cur_order    OUT refcur,
                     i_updated_from VARCHAR2,
                     i_updated_to   VARCHAR2,
                     i_created_from VARCHAR2,
                     i_created_to   VARCHAR2,
                     i_state        VARCHAR2,
                     o_data         OUT VARCHAR2,
                     o_code         OUT NUMBER) AS
l_startNum NUMBER;
l_endNum NUMBER;
l_sqlSelect VARCHAR2 (9999);
l_sqlCount VARCHAR2 (9999);
BEGIN
l_startNum := (i_page - 1) * i_limit + 1;
l_endNum := i_page * i_limit;

l_sqlSelect := 'SELECT O.*, C.KEY FROM (SELECT ID ,
                                               ORDER_NO ,
                                               ITEM_COUNT ,
                                               ITEM_TOTAL ,
                                               PROMO_TOTAL ,
                                               ADJUSTMENT_TOTAL ,
                                               TOTAL ,
                                               SHIPMENT_TOTAL ,
                                               PAYMENT_TOTAL ,
                                               CURRENCY_ID ,
                                               STATE ,
                                               SHIPMENT_STATE ,
                                               PAYMENT_STATE ,
                                               USER_ID ,
                                               COMPLETED_AT ,
                                               EMAIL ,
                                               PHONE ,
                                               REMARK ,
                                               CONFIRMATION ,
                                               CHANNEL_ID ,
                                               MERCHANT_ID ,
                                               CALLBACK_URL ,
                                               CONTINUE_URL ,
                                               CREATED_AT ,
                                               UPDATED_AT ,
                                               IP_ADDRESS ,
                                               URL,
                                               ROW_NUMBER() OVER (ORDER BY ID DESC) R FROM ORDERS WHERE MERCHANT_ID = '||i_merchant_id||' AND 1 = 1 ';

l_sqlCount := 'SELECT COUNT(*) FROM ORDERS WHERE MERCHANT_ID = '||i_merchant_id||' AND 1 = 1 ';

IF i_created_from IS NOT NULL OR i_created_from <> '' THEN
  l_sqlSelect := l_sqlSelect || ' AND CREATED_AT >= ''' || to_date(i_created_from,'DD-MM-YYYY') || '''';
  l_sqlCount := l_sqlCount || ' AND CREATED_AT >= ''' || to_date(i_created_from,'DD-MM-YYYY') || '''';
END IF;

IF i_created_to IS NOT NULL OR i_created_to <> '' THEN
  l_sqlSelect := l_sqlSelect || ' AND CREATED_AT <= ''' || to_date(i_created_to,'DD-MM-YYYY') || '''';
  l_sqlCount := l_sqlCount || ' AND CREATED_AT <= ''' || to_date(i_created_to,'DD-MM-YYYY') || '''';
END IF;

IF i_updated_from IS NOT NULL OR i_updated_from <> '' THEN
  l_sqlSelect := l_sqlSelect || ' AND UPDATED_AT >= ''' || to_date(i_updated_from,'DD-MM-YYYY') || '''';
  l_sqlCount := l_sqlCount || ' AND UPDATED_AT >= ''' || to_date(i_updated_from,'DD-MM-YYYY') || '''';
END IF;

IF i_updated_to IS NOT NULL OR i_updated_to <> '' THEN
  l_sqlSelect := l_sqlSelect || ' AND UPDATED_AT <= ''' || to_date(i_updated_to,'DD-MM-YYYY') || '''';
  l_sqlCount := l_sqlCount || ' AND UPDATED_AT <= ''' || to_date(i_updated_to,'DD-MM-YYYY') || '''';
END IF;

IF i_state IS NOT NULL OR i_state <> '' THEN
  IF UPPER(i_state) = 'OPEN' THEN
    l_sqlSelect := l_sqlSelect || ' AND (STATE = 1 OR STATE = 2) ';
    l_sqlCount := l_sqlCount || ' AND (STATE = 1 OR STATE = 2) ';
  ELSIF UPPER(i_state) = 'PENDING_TO_PAY' THEN
    l_sqlSelect := l_sqlSelect || ' AND (STATE = 3 OR STATE = 4) AND (PAYMENT_STATE = 1 OR PAYMENT_STATE = 2) ';
    l_sqlCount := l_sqlCount || ' AND (STATE = 3 OR STATE = 4) AND (PAYMENT_STATE = 1 OR PAYMENT_STATE = 2) ';
  ELSIF UPPER(i_state) = 'PENDING_TO_SHIP' THEN
    l_sqlSelect := l_sqlSelect || ' AND (STATE = 3 OR STATE = 4 OR STATE = 5) AND PAYMENT_STATE = 3 AND (SHIPMENT_STATE = 1 OR SHIPMENT_STATE = 2) ';
    l_sqlCount := l_sqlCount || ' AND (STATE = 3 OR STATE = 4 OR STATE = 5) AND PAYMENT_STATE = 3 AND (SHIPMENT_STATE = 1 OR SHIPMENT_STATE = 2) ';
  ELSIF UPPER(i_state) = 'CLOSED' THEN
    l_sqlSelect := l_sqlSelect || ' AND STATE = 6 AND (PAYMENT_STATE = 3 OR PAYMENT_STATE = 6) ';
    l_sqlCount := l_sqlCount || ' AND STATE = 6 AND (PAYMENT_STATE = 3 OR PAYMENT_STATE = 6) ';
  ELSIF UPPER(i_state) = 'CANCELED' THEN
    l_sqlSelect := l_sqlSelect || ' AND SHIPMENT_STATE = 5 ';
    l_sqlCount := l_sqlCount || ' AND SHIPMENT_STATE = 5 ';
  END IF;
END IF;

l_sqlSelect := l_sqlSelect || ' ) O INNER JOIN CATEGORIES C ON O.CURRENCY_ID = C.ID
                                  WHERE O.R >= ' || l_startNum || ' AND O.R <= ' || l_endNum;

OPEN o_cur_order FOR l_sqlSelect;
EXECUTE IMMEDIATE l_sqlCount INTO o_total_record;

  IF o_total_record = 0 THEN
    o_data := 'Không tìm thấy bản ghi nào!';
    o_code := 400;
  ELSE
    o_data := 'Success';
    o_code := 200;
  END IF;

EXCEPTION
  WHEN OTHERS 
    THEN
    DBMS_OUTPUT.put_line ('Error stack at top level:');
END GetAllOrders;

0 个答案:

没有答案