Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

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

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:com.nec.crud.CrudRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from w  w  w .j a  va 2  s  .com*/
public void refresh(final Session session, T entity) {
    if (entity != null && isAttached(session, entity)) {
        session.refresh(entity);
    }
}

From source file:com.nec.harvest.service.impl.SerialNumberServiceImpl.java

License:Open Source License

@Override
public synchronized String generateUUIDByRecID(String recID) throws ServiceException {
    Session session = null;
    Transaction tx = null;// w  w  w.  j ava2  s.com

    // New an instance
    SerialNumber serialNumber = null;
    // Trying to find the actual serial number by RecID {}
    logger.info("Trying to find the actual serial number by RecID {}", recID);

    try {
        session = HibernateSessionManager.getSession();
        tx = session.beginTransaction();
        serialNumber = (SerialNumber) session.get(SerialNumber.class, recID);
        tx.commit();
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException("An error occurred while trying to get serial number by recId " + recID, ex);
    } finally {
        tx = null;
        HibernateSessionManager.closeSession(session);
    }

    // If could not find any matched record, then create new one
    // with value is 0000000000000001
    if (serialNumber == null) {
        final String REC_NO = "0000000000000001";
        serialNumber = new SerialNumber(recID, REC_NO);

        // Create a new one serial number for RecID {}
        logger.info("Create a new one serial number for RecID {}", recID);

        try {
            // Create a new one transaction
            session = HibernateSessionManager.getSession();
            tx = session.beginTransaction();
            session.save(serialNumber);
            tx.commit();
            return REC_NO;
        } catch (HibernateException ex) {
            if (tx != null) {
                tx.rollback();
            }
            throw new ServiceException(
                    "An error occurred while trying to get maximum serial number by recId " + recID, ex);
        } finally {
            tx = null;
            HibernateSessionManager.closeSession(session);
        }
    }

    // Must be fetched the new instance of serial number directly from underlying database
    logger.info("Must be fetched the new instance of serial number directly from underlying database");

    try {
        session = HibernateSessionManager.getSession();
        tx = session.beginTransaction();
        if (session.contains(serialNumber)) {
            session.refresh(serialNumber);
        }
        tx.commit();
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }

        logger.warn(ex.getMessage(), ex);
    } finally {
        tx = null;
        HibernateSessionManager.closeSession(session);
    }

    // Actual Seq Number
    long actualSeqNo = serialNumber.getCurrentSeqNo();
    // auto increase the sequence number
    actualSeqNo += 1;

    // Substring to get the actual table name
    String tableName = null;
    int indexOfDot = recID.indexOf(".");
    if (StringUtils.isNotEmpty(recID) && indexOfDot > 1) { // e.g: AT011.RecID
        tableName = recID.substring(0, indexOfDot);
    }

    logger.info("Trying to find the maximum sequence number already existing in the table {}", tableName);

    // If the maximum sequence number already existing in the table
    // , so have to automatically increase the sequence number until
    // its value not exist in that table
    if (StringUtils.isNotEmpty(tableName)) {
        try {
            session = HibernateSessionManager.getSession();
            tx = session.beginTransaction();
            Query query = session.createSQLQuery("SELECT MAX(RecID) AS MAX_RecID FROM " + tableName);

            // Actual maximum Seq number
            String actualMaximumSeqNo = (String) query.uniqueResult();
            if (StringUtils.isNotEmpty(actualMaximumSeqNo)) {
                long actualMaxSeqNo = Long.valueOf(actualMaximumSeqNo);
                if (actualSeqNo < actualMaxSeqNo) {
                    logger.info("The maximum sequence number already existing in the {} is {}", tableName,
                            actualMaximumSeqNo);

                    // 
                    actualSeqNo = actualMaxSeqNo + 1;
                }
            }
            tx.commit();
        } catch (HibernateException ex) {
            logger.warn(ex.getMessage());

            if (tx != null) {
                tx.rollback();
            }
        } finally {
            tx = null;
            HibernateSessionManager.closeSession(session);
        }
    }

    // Format the actual sequence number from {} to {}
    String maximumSeqNo = StringUtil.numberToStringWithUserFillUp(Constants.DEFAULT_SEQUENCE_CHARACTER_FILL,
            Constants.DEFAULT_SEQUENCE_LENGTH, actualSeqNo);
    logger.info("Format the actual sequence number from {} to {}", actualSeqNo, maximumSeqNo);

    try {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        serialNumber.setSeqNo(maximumSeqNo);
        serialNumber.setTimeU(calendar);

        session = HibernateSessionManager.getSession();
        tx = session.beginTransaction();
        session.update(serialNumber);
        tx.commit();

        // The current serial number is {}
        logger.info("The current serial number is {}", maximumSeqNo);
        return maximumSeqNo;
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
        }
        throw new ServiceException(
                "An error occurred while trying to get maximum serial number by recId " + recID, ex);
    } finally {
        tx = null;
        HibernateSessionManager.closeSession(session);
    }
}

From source file:com.sigera.dao.impl.MaterialImp.java

@Override
public void recargarEntidad(Material t) {
    Session session = null;
    session = sessionFactory.getCurrentSession();
    session.refresh(t);
}

From source file:com.sigera.dao.impl.ModuloImp.java

@Override
public void recargarEntidad(Modulo t) {
    Session session = null;
    session = sessionFactory.getCurrentSession();
    session.refresh(t);
}

From source file:com.sigera.dao.impl.PermisoImp.java

@Override
public void recargarEntidad(Permiso t) {
    Session session = null;
    session = sessionFactory.getCurrentSession();
    session.refresh(t);
}

From source file:com.smi.travel.datalayer.dao.impl.PaymentStockImpl.java

@Override
public String insertOrUpdatePaymentStock(PaymentStock paymentStock) {
    String result = "";
    boolean checkupdate = false;
    try {//w w  w.  j av  a2 s . co m
        Session session = this.sessionFactory.openSession();
        transaction = session.beginTransaction();
        PaymentStock payment = (PaymentStock) session.load(PaymentStock.class, paymentStock.getId());
        session.refresh(payment);
        if ("".equalsIgnoreCase(paymentStock.getId()) || paymentStock.getId() == null) {
            result = generatePayNo();
            paymentStock.setPayStockNo(result);
            session.save(paymentStock);

            List<PaymentStockDetail> paymentStockDetails = paymentStock.getPaymentStockDetails();
            if (paymentStockDetails != null) {
                for (int i = 0; i < paymentStockDetails.size(); i++) {
                    session.save(paymentStockDetails.get(i));
                    List<PaymentStockItem> paymentStockItems = paymentStockDetails.get(i)
                            .getPaymentStockItems();
                    if (paymentStockItems != null) {
                        for (int j = 0; j < paymentStockItems.size(); j++) {
                            session.save(paymentStockItems.get(j));
                        }
                    }
                }
            }
        } else {
            System.out.println("+++++++++++++++++++++ UPDATE +++++++++++++++++++++ ");
            session.update(paymentStock);
            List<PaymentStockDetail> paymentStockDetails = paymentStock.getPaymentStockDetails();

            if (paymentStockDetails != null && paymentStockDetails.size() != 0) {
                System.out.println(" paymentStockDetails.size()  " + paymentStockDetails.size());
                for (int i = 0; i < paymentStockDetails.size(); i++) {
                    if (!"".equalsIgnoreCase(paymentStockDetails.get(i).getId())
                            && paymentStockDetails.get(i).getId() != null) {
                        session.update(paymentStockDetails.get(i));
                    } else {
                        session.save(paymentStockDetails.get(i));
                    }

                    List<PaymentStockItem> paymentStockItems = paymentStockDetails.get(i)
                            .getPaymentStockItems();
                    if (paymentStockItems != null && paymentStockItems.size() != 0) {
                        System.out.println("  paymentStockItems.size()  " + paymentStockItems.size());
                        for (int j = 0; j < paymentStockItems.size(); j++) {
                            if (!"".equalsIgnoreCase(paymentStockItems.get(j).getId())
                                    && paymentStockItems.get(j).getId() != null) {
                                session.update(paymentStockItems.get(j));
                            } else {
                                session.save(paymentStockItems.get(j));
                            }
                        }
                    }
                }
            }
            checkupdate = true;
        }
        transaction.commit();
        session.close();
        this.sessionFactory.close();
        if (checkupdate) {
            result = "success";
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        result = "fail";
    }
    System.out.println("result::" + result);
    return result;
}

From source file:com.thruzero.domain.hibernate.dao.HibernateGenericDAO.java

License:Apache License

public void refresh(T domainObject) {
    Session session = getCurrentSession();

    doRefresh(session, domainObject);

    session.refresh(domainObject);
}

From source file:com.vmware.thinapp.manualmode.dao.AbstractDAO.java

License:Open Source License

public void refresh(T object) {
    Session session = getCurrentSession();
    session.refresh(object);
}

From source file:com.wavemaker.runtime.data.task.BaseTask.java

License:Open Source License

protected void maybeRefreshEntity(Object o, Session session, String dbName) {
    if (getMetaData(dbName).refreshEntity(o.getClass())) {
        session.flush();//from   w w w . j av  a  2 s .c o  m
        session.refresh(o);
    }
}

From source file:controller.CommandesController.java

@RequestMapping(value = "/addCommandes", method = RequestMethod.POST)
public String ajoutCommande(Model model, Orders order, HttpServletRequest request,
        @RequestParam(value = "history", defaultValue = "") String hist1,
        @RequestParam(value = "history2", defaultValue = "") String hist2,
        @RequestParam(value = "history3", defaultValue = "") String hist3) {
    DaoOrders dao = (DaoOrders) FactoryDao.getDao(Orders.class);
    Employee emp = (Employee) request.getSession().getAttribute("userConnecte");
    order.setEmployee(emp);/*w w w. j  a  va 2 s .c o  m*/
    Date d = new Date();
    order.setDateOrders(d);
    dao.insert(order);
    List<Orders> l = new ArrayList<Orders>();
    Session s = HibernateUtil.getSession();
    s.merge(order);
    s.refresh(order);
    l.add(order);
    model.addAttribute("ordp", new Orderproduct());
    model.addAttribute("lOrder", l);
    return "addCommandes_1";
}