Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:com.nkapps.billing.dao.BankStatementDaoImpl.java

@Override
public void updateInvoiceNums(Integer year) throws Exception {
    Session sessionQuery = getSession();
    InvoiceNum in = (InvoiceNum) sessionQuery.get(InvoiceNum.class, year);
    if (in == null) {
        in = new InvoiceNum(year);
        in.setNum(0l);//from  w ww.  j  av  a2  s.  c  o  m
    }

    Long num = in.getNum();

    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy H:m:s.S");
    Date paymentDateBegin = sdf.parse("01.01." + year + " 00:00:00.000");
    Date paymentDateEnd = sdf.parse("31.12." + year + " 23:59:59.999");

    String q = " SELECT bs "
            + " FROM BankStatement bs LEFT JOIN FETCH bs.bankStatementPayments bsp LEFT JOIN FETCH bsp.id.payment p"
            + " WHERE bs.paymentDate BETWEEN :paymentDateBegin" + " AND :paymentDateEnd"
            + " AND bs.invoiceNum IS NULL" + " ORDER BY bs.paymentDate, bs.paymentSum ";
    Query query = sessionQuery.createQuery(q);
    query.setParameter("paymentDateBegin", paymentDateBegin);
    query.setParameter("paymentDateEnd", paymentDateEnd);
    List<BankStatement> bsList = query.list();

    for (BankStatement bs : bsList) {
        if (bs.getBankStatementPayments().isEmpty()) {
            bs.setInvoiceNum(++num);
        } else {
            boolean isClaim = false;
            for (BankStatementPayment bsp : bs.getBankStatementPayments()) {
                Payment payment = bsp.getId().getPayment();
                if (payment.getClaim() == 1) {
                    isClaim = true;
                    break;
                }
            }
            if (isClaim) {
                query = sessionQuery.createQuery("SELECT MAX(da.id) FROM DsApplication da WHERE tin = :tin")
                        .setParameter("tin", bs.getTin());
                Long dsApplicationId = (Long) query.uniqueResult();
                if (dsApplicationId == null) {
                    bs.setInvoiceNum(++num);
                } else {
                    bs.setInvoiceNum(dsApplicationId); // ds_application id
                }
            } else {
                bs.setInvoiceNum(++num);
            }
        }
    }
    sessionQuery.close();

    Session sessionTransaction = getSession();
    Transaction transaction = sessionTransaction.beginTransaction();

    in.setNum(num);
    sessionTransaction.saveOrUpdate(in);

    int itr = 0;
    for (BankStatement bs : bsList) {
        itr++;
        sessionTransaction.update(bs);
        if (itr % 500 == 0) {
            sessionTransaction.flush();
            sessionTransaction.clear();
        }
    }
    transaction.commit();
    sessionTransaction.close();

}

From source file:com.nkapps.billing.dao.OverpaymentDaoImpl.java

@Override
public String singleReturnBankStatementText(String bankStatementId, Long issuerSerialNumber, String issuerIp)
        throws Exception {
    Session session = getSession();
    Transaction transaction = session.beginTransaction();
    StringBuilder result = new StringBuilder();

    int year = Calendar.getInstance().get(Calendar.YEAR);
    ReturnNum rn = (ReturnNum) session.get(ReturnNum.class, year);
    if (rn == null) {
        rn = new ReturnNum(year);
        rn.setNum(0l);/*from w  ww.  ja  v  a 2 s  . co  m*/
    }
    Long num = rn.getNum();

    BankStatement bs = (BankStatement) session.get(BankStatement.class, bankStatementId);
    if (bs.getReturnState() == 1 || bs.getReturnState() == 2) {
        Date returnDate = Calendar.getInstance().getTime();
        LocalDateTime dateTime = LocalDateTime.now();

        bs.setReturnState((short) 2);
        if (bs.getReturnNum() == null) {
            bs.setReturnNum(++num);
        }
        bs.setReturnDate(returnDate);
        bs.setIssuerSerialNumber(issuerSerialNumber);
        bs.setIssuerIp(issuerIp);
        bs.setDateUpdated(dateTime);

        session.update(bs);

        result.append(bankStatementReturnText(bs));
    }

    rn.setNum(num);
    session.saveOrUpdate(rn);

    transaction.commit();
    session.close();
    return result.toString();
}

From source file:com.nkapps.billing.dao.OverpaymentDaoImpl.java

@Override
public String allReturnBankStatementText(Date paymentDate, Long issuerSerialNumber, String issuerIp)
        throws Exception {
    Session session = getSession();
    StringBuilder result = new StringBuilder();

    int year = Calendar.getInstance().get(Calendar.YEAR);
    ReturnNum rn = (ReturnNum) session.get(ReturnNum.class, year);
    if (rn == null) {
        rn = new ReturnNum(year);
        rn.setNum(0l);/*from w w w.  j av  a  2s.  com*/
    }
    Long num = rn.getNum();

    Query query = session.createQuery(
            "SELECT bs FROM BankStatement bs WHERE bs.paymentDate = :paymentDate AND bs.returnState IN (1,2)");
    query.setParameter("paymentDate", paymentDate);
    List<BankStatement> bsList = query.list();
    session.close();

    session = getSession();
    Transaction transaction = session.beginTransaction();

    Date returnDate = Calendar.getInstance().getTime();
    LocalDateTime dateTime = LocalDateTime.now();

    for (BankStatement bs : bsList) {
        bs.setReturnState((short) 2);
        if (bs.getReturnNum() == null) {
            bs.setReturnNum(++num);
        }
        bs.setReturnDate(returnDate);
        bs.setIssuerSerialNumber(issuerSerialNumber);
        bs.setIssuerIp(issuerIp);
        bs.setDateUpdated(dateTime);

        session.update(bs);

        result.append(bankStatementReturnText(bs));
    }

    rn.setNum(num);
    session.saveOrUpdate(rn);

    transaction.commit();
    session.close();
    return result.toString();
}

From source file:com.npower.dm.audit.hibernate.BaseAuditLogger.java

License:Open Source License

protected long save(AuditLogEntity log, AuditLogTargetEntity target) throws AuditException {
    AuditLoggerFactoryImpl factoryImpl = (AuditLoggerFactoryImpl) this.getFactory();

    Session hsession = null;
    Transaction tx = null;/*from w w  w.j a  v a2s .  c o  m*/
    try {
        hsession = factoryImpl.getHibernateSession();
        tx = hsession.beginTransaction();

        hsession.saveOrUpdate(log);

        if (target != null) {
            target.setActionLog(log);
            hsession.saveOrUpdate(target);
        }
        tx.commit();

        return log.getID();
    } catch (Exception ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new AuditException("Failure in output log.", ex);
    } finally {
        if (hsession != null) {
            factoryImpl.releaseSession(hsession);
        }
    }
}

From source file:com.npower.dm.hibernate.management.AutomaticProvisionJobBeanImpl.java

License:Open Source License

/**
 * @param jobID/*from  ww  w.j  a v  a2s  .c o  m*/
 * @param newState
 * @throws DMException
 */
private void updateJobState(long jobID, String newState) throws DMException {
    try {
        AutomaticProvisionJob job = this.loadJobByID(jobID);
        job.setState(newState);
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);

    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.AutomaticProvisionJobBeanImpl.java

License:Open Source License

public AutomaticProvisionJob newJob4Assignment(AutomaticProvisionJobSelector jobSelector,
        ProfileConfig[] profiles) throws DMException {
    if (profiles == null || profiles.length == 0) {
        throw new DMException("Must specified profiles for automatic provision job.");
    }//from  w ww .  j  a  v a  2  s  .  c om

    try {
        Session session = this.getHibernateSession();
        AutoProvisionJobEntity job = new AutoProvisionJobEntity();
        job.setJobSelector(jobSelector);
        job.setType(AutomaticProvisionJob.TYPE_AUTO_REG);
        job.setJobType(AutomaticProvisionJob.JOB_TYPE_ASSIGN_PROFILE);
        job.setJobTypeForDisplay(AutomaticProvisionJob.JOB_TYPE_ASSIGN_PROFILE);
        job.setState(AutomaticProvisionJob.JOB_STATE_APPLIED);
        job.setBeginTime(new Date());
        session.saveOrUpdate(job);

        for (int i = 0; i < profiles.length; i++) {
            if (profiles[i] == null) {
                continue;
            }
            AutoJobProfileConfigId id = new AutoJobProfileConfigId(job, (long) i);
            AutoJobProfileConfig profileJob = new AutoJobProfileConfig(id, profiles[i]);
            session.saveOrUpdate(profileJob);
            job.getAutoJobProfileConfigs().add(profileJob);
        }
        return job;
    } catch (HibernateException e) {
        throw new DMException(e);
    } catch (Exception e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.AutomaticProvisionJobBeanImpl.java

License:Open Source License

public AutomaticProvisionJob newJob4Command(AutomaticProvisionJobSelector jobSelector, String scripts)
        throws DMException {
    if (StringUtils.isEmpty(scripts)) {
        throw new DMException("Must specified scripts for automatic provision job.");
    }//from   w w  w. j  a v a2 s. c  o m

    try {
        Session session = this.getHibernateSession();
        AutoProvisionJobEntity job = new AutoProvisionJobEntity();

        job.setJobSelector(jobSelector);
        job.setType(AutomaticProvisionJob.TYPE_AUTO_REG);
        job.setJobType(AutomaticProvisionJob.JOB_TYPE_SCRIPT);
        job.setJobTypeForDisplay(AutomaticProvisionJob.JOB_TYPE_SCRIPT);
        job.setState(AutomaticProvisionJob.JOB_STATE_APPLIED);
        job.setBeginTime(new Date());
        job.setScript(scripts);
        session.saveOrUpdate(job);

        return job;
    } catch (HibernateException e) {
        throw new DMException(e);
    } catch (Exception e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.AutomaticProvisionJobBeanImpl.java

License:Open Source License

public AutomaticProvisionJob newJob4Discovery(AutomaticProvisionJobSelector jobSelector, String[] nodePaths)
        throws DMException {
    if (nodePaths == null || nodePaths.length == 0) {
        throw new DMException("Must specified node path for automatic provision job.");
    }/*from w w  w  . j  a  va 2  s  .com*/

    try {
        Session session = this.getHibernateSession();
        AutoProvisionJobEntity job = new AutoProvisionJobEntity();
        job.setJobSelector(jobSelector);
        job.setType(AutomaticProvisionJob.TYPE_AUTO_REG);
        job.setJobType(AutomaticProvisionJob.JOB_TYPE_DISCOVERY);
        job.setJobTypeForDisplay(AutomaticProvisionJob.JOB_TYPE_DISCOVERY);
        job.setState(AutomaticProvisionJob.JOB_STATE_APPLIED);
        job.setBeginTime(new Date());
        session.saveOrUpdate(job);

        for (int i = 0; i < nodePaths.length; i++) {
            if (StringUtils.isEmpty(nodePaths[i])) {
                continue;
            }
            AutoJobNodesDiscoverId id = new AutoJobNodesDiscoverId(job, (long) i);
            AutoJobNodesDiscover discovery = new AutoJobNodesDiscover(id, nodePaths[i]);
            session.saveOrUpdate(discovery);
            job.getAutoJobNodesDiscovers().add(discovery);
        }
        return job;
    } catch (HibernateException e) {
        throw new DMException(e);
    } catch (Exception e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.AutomaticProvisionJobBeanImpl.java

License:Open Source License

public void update(AutomaticProvisionJob job) throws DMException {
    try {/*from   ww w .  ja va2 s.co  m*/
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.ClientProvTemplateBeanImpl.java

License:Open Source License

public void update(ClientProvTemplate template) throws DMException {
    if (template == null) {
        throw new NullPointerException("Could not add a null ClientProvTemplate into database.");
    }//from  w ww. j  a  va2 s  . com
    Session session = this.getHibernateSession();
    try {
        session.saveOrUpdate(template);
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}