Java Code Examples for org.hibernate.Session.merge()

The following are Jave code examples for showing how to use merge() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Celebino   File: GenericDao.java   View Source Code Vote up 6 votes
public Long insert(T entity) throws SQLException {
	Session session = HibernateUtil.getSessionFactory().openSession();
	Long id;

	try {
		session.beginTransaction();
		id = (Long) session.merge(entity);
		session.getTransaction().commit();

	} catch (HibernateException hibernateException) {
		session.getTransaction().rollback();

		throw new SQLException(hibernateException);

	} finally {
		session.close();
	}

	return id;
}
 
Example 2
Project: tinyshop8   File: GoodsDAOImpl.java   View Source Code Vote up 5 votes
/**
 * 新增商品信息
 *
 * @param goods8     商品实体类
 * @param goodsType8 商品类型实体类
 * @return 返回新增具体类
 */
@Override
public FrameWorkDSCResult addGoods(final Goods8JPA goods8,
                                   final GoodsType8JPA goodsType8)
{
    FrameWorkDSCResult result = new FrameWorkDSCResult();

    try
    {
        setSession(null);
        Session session = SessionBasic.session;

        //User8JPA user = goods8.getUser8JPA();
        //user.getGoods8JPASet().add(goods8);
        //goods8.setUser8JPA(user);
        //goods8.setGoodsType(goodsType8);
        //goodsType8.getGoods8JPASet().add(goods8);

        session.merge(goods8);
        //更新商品类型信息
        session.refresh(goodsType8);
        //session.refresh(user);

        getTransaction().commit();
        result.setDaoFlag(true);
    }
    catch (Exception ex)
    {
        result.setDaoFlag(false);
        printStackTrace(ex);
        result.setDaoException(ex);
    }
    finally
    {
        return result;
    }
}
 
Example 3
Project: TrabalhoCrisParte2   File: DAOFornecedor.java   View Source Code Vote up 4 votes
public void alterar(Fornecedor fornecedor) {

       Session session = DAOHibernateUtil.getSession();

       try {

           session.beginTransaction(); // Abre-se uma transação
           session.merge(fornecedor);      // Acumula a operação de alteração do objeto produto no BD, na transação
           session.getTransaction().commit();   // Realiza definitivamente todas as operações pendentes na transação
           JOptionPane.showMessageDialog(null, "Fornecedor alterado com sucesso");

       } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex);

       }
   }
 
Example 4
Project: TrabalhoCrisParte2   File: DAOProduto.java   View Source Code Vote up 4 votes
public void alterar(Produto produto) {

       Session session = DAOHibernateUtil.getSession();

       try {

           session.beginTransaction(); // Abre-se uma transação
           session.merge(produto);      // Acumula a operação de alteração do objeto produto no BD, na transação
           session.getTransaction().commit();   // Realiza definitivamente todas as operações pendentes na transação
           JOptionPane.showMessageDialog(null, "Produto alterado com sucesso");

       } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex);

       }
   }
 
Example 5
Project: TrabalhoCrisParte2   File: DAOEntrada.java   View Source Code Vote up 4 votes
public void alterar(Entrada entrada) {

       Session session = DAOHibernateUtil.getSession();

       try {

           session.beginTransaction(); // Abre-se uma transação
           session.merge(entrada);      // Acumula a operação de alteração do objeto entrada no BD, na transação
           session.getTransaction().commit();   // Realiza definitivamente todas as operações pendentes na transação
           JOptionPane.showMessageDialog(null, "Entrada alterada com sucesso");

       } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex);

       }
   }
 
Example 6
Project: TrabalhoCrisParte2   File: DAOCliente.java   View Source Code Vote up 4 votes
public void alterar(Cliente cliente) {

       Session session = DAOHibernateUtil.getSession();

       try {

           session.beginTransaction(); // Abre-se uma transação
           session.merge(cliente);      // Acumula a operação de alteração do objeto produto no BD, na transação
           session.getTransaction().commit();   // Realiza definitivamente todas as operações pendentes na transação
           JOptionPane.showMessageDialog(null, "Cliente alterado com sucesso");

       } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex);

       }
   }
 
Example 7
Project: sjk   File: MarketDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void merge(Session session, Market market) {
    session.merge(market);
}
 
Example 8
Project: sjk   File: MarketDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void merge(Session session, Market market) {
    session.merge(market);
}
 
Example 9
Project: sjk   File: ApiImporterMarketServiceImpl.java   View Source Code Vote up 4 votes
@Override
public void importIncrement() {
    logger.info("{}. ImportIncrement begins...", this.getMarketName());
    Session session = null;
    Transaction tx = null;
    boolean successed = false;
    Date now = new Date();
    do {
        session = sessions.openSession();
        session.setDefaultReadOnly(false);
        try {
            Market market = null;
            try {
                market = getMarket(session);
            } finally {
                session.close();
            }
            int currentPage = 0, totalPage = 0;
            oneIncrement: do {
                PaginationMarketApp increment = getMarketAppForIncrement(market);
                if (increment != null) {
                    currentPage = increment.getCurrentPage();
                    totalPage = increment.getTotalPages();
                    List<MarketApp> marketApps = increment.getData();
                    List<MarketApp> offMarketApps = new ArrayList<MarketApp>();
                    if (marketApps != null && !marketApps.isEmpty()) {
                        // open again
                        if (!session.isOpen()) {
                            session = sessions.openSession();
                        }
                        tx = session.beginTransaction();
                        savePaginationMarketApp(session, market, marketApps, offMarketApps);
                        market.setIncrementLastReqCurrentPage(currentPage);
                        market.setIncrementTotalPages(totalPage);
                        session.merge(market);
                        tx.commit();
                        tx = null;
                        session.close();
                        deleteMarketAppsTransaction(offMarketApps);
                        logger.info(
                                "{}. Increment totalPage: {}\t currentPage: {}\t size: {}\t offMarketApps size: {}",
                                this.getMarketName(), totalPage, currentPage, marketApps.size(),
                                offMarketApps.size());
                        marketApps.clear();
                        offMarketApps.clear();
                    }
                    if (currentPage >= totalPage) {
                        successed = true;
                    }
                } else {
                    successed = false;
                    break oneIncrement;
                }
            } while (totalPage > 0 && currentPage < totalPage);
            if (successed) {
                resetMarketForIncrement(now);
            } else {
                sleepForTry();
            }
        } catch (Exception e) {
            successed = false;
            if (tx != null) {
                tx.rollback();
            }
            logger.error("Exception", e);
            sleepForTry();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    } while (!successed);
    logger.info("{}. ImportIncrement done!", this.getMarketName());
}
 
Example 10
Project: sjk   File: ApiImporterMarketServiceImpl.java   View Source Code Vote up 4 votes
private PaginationMarketApp savePaginationForFull() throws Exception {
    // saveOrUpdate to table MarketApp
    PaginationMarketApp marketPagination = null;
    Session session = sessions.openSession();
    session.setDefaultReadOnly(false);
    Transaction tx = null;
    try {
        Market market = getMarket(session);
        marketPagination = getMarketAppForFull(market);
        if (marketPagination != null && marketPagination.getTotalPages() >= marketPagination.getCurrentPage()) {
            List<MarketApp> marketApps = marketPagination.getData();
            List<MarketApp> offMarketApps = new ArrayList<MarketApp>();
            if (marketApps != null && !marketApps.isEmpty()) {
                Object[] infos = new Object[] { market.getMarketName(), marketPagination.getTotalPages(),
                        marketPagination.getCurrentPage(), marketApps.size() };
                accessMarketDao.getMarketlogger().info(
                        "Begin to import {} on one page. totalPages: {} currentPage : {} , size: {}", infos);
                tx = session.beginTransaction();
                savePaginationMarketApp(session, market, marketApps, offMarketApps);
                market.setFullLastReqCurrentPage(marketPagination.getCurrentPage());
                market.setFullTotalPages(marketPagination.getTotalPages());
                market.setFullLastTime(new Date());
                session.merge(market);
                tx.commit();
                tx = null;
                session.clear();
                session.close();
                deleteMarketAppsTransaction(offMarketApps);
                marketApps.clear();
                offMarketApps.clear();
            }
        }
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        logger.error("Exception", e);
        throw e;
    } finally {
        if (session.isOpen()) {
            session.close();
        }
    }
    return marketPagination;
}
 
Example 11
Project: TrabalhoCrisParte2   File: DAOEntradaProduto.java   View Source Code Vote up 3 votes
public void alterar(EntradaProduto entradaProduto) {

       Session session = DAOHibernateUtil.getSession();

       try {

           session.beginTransaction(); // Abre-se uma transação
           session.merge(entradaProduto);      // Acumula a operação de alteração do objeto entrada no BD, na transação
           session.getTransaction().commit();   // Realiza definitivamente todas as operações pendentes na transação

       } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex);

       }
   }