entityManager createNativeQuery自动提交

时间:2016-05-03 06:17:17

标签: hibernate jpa commit entitymanager flush

<?php

$tbl[]="
CREATE TABLE IF NOT EXISTS ".$table_prefix."admin_user (
  `admin_id` int(14) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(100) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `role` varchar(100) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  `ip_address` varchar(50) NOT NULL,  
  `user_lang` int(14) NOT NULL,
  `email_address` varchar(255) NOT NULL,
  `lost_password_code` varchar(255) NOT NULL,
  `session_token` varchar(255) NOT NULL,
  `last_login` datetime NOT NULL,
  `user_access` text NOT NULL,
  PRIMARY KEY (`admin_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
";

/*insert user account*/

     $params_user=array( 
    'username'=>$data['username'],
    'password'=>md5($data['password']),
    'date_created'=>date('c'),
    'ip_address'=>$_SERVER['REMOTE_ADDR'],
    'user_access'=>$user_all_access
  );
  echo "<p class=\"uk-icon-chevron-right\"> Inserting user ...</p>";      
  $truncate="TRUNCATE TABLE ".$table_prefix."admin_user";   
  $db->raw($truncate);
  $db->create( $table_prefix."admin_user", $params_user); 
  echo "<div> [done]</div>";

以上代码我用于在DB中插入或更新。 每个函数包含2-5个createNativeQuery。 似乎在每个本机查询之后它会自动提交。 但我在任何查询中都有异常没有回滚但抛出异常如:

try {
        EntityTransaction tx = entityManager.getTransaction();
        try {
            tx.begin();
            // insert in process_loan_ledger
                this.saveProcessLoanLedger(ledgerMonth);
                // insert into loan_ledger from process_loan_ledger
                this.saveLoanLedger(ledgerMonth);
                // delete loans from loan_ledger where emp code like '9-99%'
                this.deleteFromLoanLedger(ledgerMonth);
                // insert into Emp ledger from loan ledger
                this.saveLoansIntoEmpLedger(ledgerMonth, role);
            tx.commit();
           }
    } catch (Exception x) {
               -----rollback-------
            }
        } finally {
            -----entity manager closed-------
        }

我想做tx.commit();否则回滚。

其中一种方法:

javax.persistence.RollbackException: Transaction marked as rollbackOnly

堆栈跟踪:

public void saveProcessLoanLedger(Integer ledgerMonth) {
    try {
        /**
         * truncate PROCESS_LOAN_LEDGER
         */
        entityManager.createNativeQuery("TRUNCATE TABLE PROCESS_LOAN_LEDGER").executeUpdate();

        /**
         * insert into PROCESS_LOAN_LEDGER
         */
        entityManager.createNativeQuery("INSERT"
                + " INTO PROCESS_LOAN_LEDGER"
                + " SELECT NULL AS \"ID\" ,"
                + "  :ledgMonth AS LEDGER_MONTH ,"
                + "  LOANS.BAL ,"
                + "  LOANS.CUM_INTR ,"
                + "  LOANS.GRACE_PERIOD,"
                + "  LOANS.INSTALL ,"
                + "  LOANS.INSTALL_NO ,"
                + "  LOANS.INT ,"
                + "  LOANS.IS_CUR_STATUS ,"
                + "  LOANS.PRINCIPAL ,"
                + "  LOANS.LOANTYPES_ID ,"
                + "  LOANS.EMPLOYEES_ID ,"
                + "  LOANS.LOAN_UID ,"
                + "  'System' AS CREATED_BY ,"
                + "  'System' AS UDPATED_BY ,"
                + "  LOANS.BAL ,"
                + "  TRUNC((LOANS.BAL * LOAN_TYPES.INTEREST_RATE) / 1200.0, 2) AS CALCULATED_INT, "
                + "  :currentTimeStamp ,"
                + "  :currentTimeStamp "
                + " FROM"
                + " LOANS"
                + " INNER JOIN LOAN_TYPES"
                + " ON (LOANS.LOANTYPES_ID = LOAN_TYPES.\"ID\")"
                + " WHERE IS_CUR_STATUS = 0").setParameter("currentTimeStamp", new java.sql.Timestamp(currentTimeStamp.getTime())).setParameter("ledgMonth", ledgerMonth).executeUpdate();

        /**
         * update into PROCESS_LOAN_LEDGER
         */
        entityManager.createNativeQuery("UPDATE PROCESS_LOAN_LEDGER"
                + " SET INSTALL = LEAST(INSTALL, BAL)"
                + " WHERE GRACE_PERIOD = 0"
                + " AND IS_CUR_STATUS = 0").executeUpdate();

        entityManager.createNativeQuery("UPDATE PROCESS_LOAN_LEDGER"
                + " SET INT = LEAST(INT, CUM_INTR)"
                + " WHERE GRACE_PERIOD = 0"
                + " AND INSTALL = 0"
                + " AND IS_CUR_STATUS = 0").executeUpdate();

        //check for action 6 loan if value = 1 then int is 0
        try {
            Integer action6Loan = Integer.parseInt((String) entityManager.createNativeQuery("select value from PAYROLL_SETTINGS where id = 'Action_6_loan'").getSingleResult());
            if (action6Loan == 1) {
                entityManager.createNativeQuery("UPDATE PROCESS_LOAN_LEDGER "
                        + "SET CALCULATED_INT = 0 "
                        + "WHERE EMPLOYEES_ID IN "
                        + "  (SELECT ID "
                        + "   FROM "
                        + "    ("
                        + this.getAction6ForPRL()
                        + "    ) "
                        + "  )").setParameter("ledgMonth", ledgerMonth).executeUpdate();
            }
        } catch (Exception x) {
        }

        // guaranter loan interest = 0 
        entityManager.createNativeQuery("update PROCESS_LOAN_LEDGER set CALCULATED_INT = 0 where LOAN_UID like '%:G%'").executeUpdate();

        //action 8 set install=0, int=0, CALCULATED_INT=0
        entityManager.createNativeQuery("update PROCESS_LOAN_LEDGER "
                + "set install=0, int=0, CALCULATED_INT=0 "
                + "WHERE EMPLOYEES_ID IN "
                + "  (SELECT ie.id "
                + "  FROM EMPLOYEES ie "
                + "  INNER JOIN "
                + "    (SELECT EMPLOYEES_ID, "
                + "      ACTION "
                + "    FROM "
                + "      (SELECT ep.EMPLOYEES_ID, "
                + "        CASE "
                + "          WHEN es.ACTION IS NULL "
                + "          THEN ep.ACTION "
                + "          ELSE ES.ACTION "
                + "        END ACTION "
                + "      FROM EMP_PAYSCALES ep "
                + "      LEFT JOIN EMP_STATUS es "
                + "      ON (ep.EMPLOYEES_ID = es.EMPLOYEES_ID "
                + "      AND es.ledger_month = :ledgMonth) "
                + "      ) "
                + "    WHERE ACTION IN (8) "
                + "    ) ies ON (ie.ID   = IES.EMPLOYEES_ID) "
                + "  )").setParameter("ledgMonth", ledgerMonth).executeUpdate();

        entityManager.createNativeQuery("UPDATE PROCESS_LOAN_LEDGER"
                + " SET INT = CALCULATED_INT"
                + " WHERE LOAN_UID = 'LB'"
                + " AND IS_CUR_STATUS = 0").executeUpdate();

        entityManager.createNativeQuery("UPDATE PROCESS_LOAN_LEDGER"
                + " SET BAL = BAL - INSTALL,"
                + " CUM_INTR = CUM_INTR - INT + CALCULATED_INT"
                + " WHERE IS_CUR_STATUS = 0").executeUpdate();

        entityManager.createNativeQuery("delete from process_loan_ledger where (bal=0 or bal is null) and (cum_intr=0 or cum_intr is null) and (INSTALL=0 or INSTALL is null) and (INT=0 or INT is null)").executeUpdate();

        //delete employees who is 5
        entityManager.createNativeQuery("DELETE "
                + "FROM process_loan_ledger "
                + "WHERE employees_id IN "
                + "  (SELECT DISTINCT(EMPLOYEES_ID) FROM PROCESS_LOAN_LEDGER "
                + "  MINUS "
                + "  SELECT ie.id "
                + "  FROM EMPLOYEES ie "
                + "  INNER JOIN "
                + "    (SELECT EMPLOYEES_ID, "
                + "      ACTION "
                + "    FROM "
                + "      (SELECT ep.EMPLOYEES_ID, "
                + "        CASE "
                + "          WHEN es.ACTION IS NULL "
                + "          THEN ep.ACTION "
                + "          ELSE ES.ACTION "
                + "        END ACTION "
                + "      FROM EMP_PAYSCALES ep "
                + "      LEFT JOIN EMP_STATUS es "
                + "      ON (ep.EMPLOYEES_ID = es.EMPLOYEES_ID "
                + "      AND es.ledger_month = :ledgMonth) "
                + "      ) "
                + "    WHERE ACTION NOT IN (5) "
                + "    ) ies ON (ie.ID   = IES.EMPLOYEES_ID) "
                + "  )").setParameter("ledgMonth", ledgerMonth).executeUpdate();

        msg = new StringBuilder().append(msg).append("Loan successfully processed<br>").toString();
    } catch (Exception x) {
        msg = new StringBuilder().append(msg).append("Loan process failed<br>").toString();
    }
}

提前致谢

0 个答案:

没有答案