Example usage for org.hibernate HibernateException HibernateException

List of usage examples for org.hibernate HibernateException HibernateException

Introduction

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

Prototype

public HibernateException(Throwable cause) 

Source Link

Document

Constructs a HibernateException using the given message and underlying cause.

Usage

From source file:controller.KoszykController.java

public String sprzedajKoszyk(ArrayList<Produkt> koszyk) {
    SessionFactory sf = SklepHibernateUtil.getSessionFactory();
    Session session = sf.openSession();//from   ww w .ja  v a2  s  .c  o  m
    Transaction tx = null;
    String userid = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
    try {
        for (int i = 0; i < koszyk.size(); i++) {
            if (koszyk.get(i).getLiczbaSztuk() > this.searchLS(koszyk.get(i).getIdProduktu())) {
                throw new HibernateException("Brak tylu produktow.");
            }
            tx = session.beginTransaction();
            SQLQuery insertQuery = session.createSQLQuery(
                    "INSERT INTO Sprzedaz(id_produktu,userid,liczba_sztuk_sprzedanych,kwota)VALUES(?,?,?,?)");
            insertQuery.setParameter(0, koszyk.get(i).getIdProduktu());
            insertQuery.setParameter(1, userid);
            insertQuery.setParameter(2, koszyk.get(i).getLiczbaSztuk());
            insertQuery.setParameter(3, koszyk.get(i).getCena() * koszyk.get(i).getLiczbaSztuk());
            insertQuery.executeUpdate();
            tx.commit();
            tx = session.beginTransaction();
            Integer minus_sztuki = koszyk.get(i).getLiczbaSztuk();
            SQLQuery removeQuery = session
                    .createSQLQuery("Update Produkt set liczba_sztuk = liczba_sztuk - ? where id_produktu = ?");
            removeQuery.setParameter(0, minus_sztuki);
            removeQuery.setParameter(1, koszyk.get(i).getIdProduktu());
            removeQuery.executeUpdate();
            tx.commit();
        }
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    this.suma = 0.0;
    this.koszyk = new ArrayList();
    return "/faces/klient/welcome.xhtml";
}

From source file:controllers.PurchaseController.java

License:Open Source License

/**
 * Generate a new purchase and bind it with the details
 *
 * @param purchaseDetails The Details list with a null purchase
 * @param userId The user that is buying
 * @return The Details list already stored binded with a purchase
 * @throws StorageException//from   w  w w . java 2 s .  c o  m
 * @throws InvalidParameterException
 */
public static List<Details> purchaseProducts(List<Details> purchaseDetails, Integer userId)
        throws StorageException, InvalidParameterException {

    // Check if there are any products to buy and store
    if (purchaseDetails.size() <= 0)
        throw new InvalidParameterException("La cantidad de productos a comprar es nula.");

    Integer productAmount, productId;

    // Get the user how made the purchase
    Users user = UsersController.getUser(userId);
    // Generate a new purchase
    Purchases purchase = new Purchases(false, user);

    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();

        // Store the purchase to have an ID ;)
        new PurchasesDaoImpl(session).add(purchase);

        Products product = null;

        // Add each product into the purchase's details
        for (Details d : purchaseDetails) {
            productId = d.getProducts().getIdProduct();
            productAmount = d.getAmount();

            // Recover product data
            product = new ProductsDaoImpl(session).get(productId);

            // Check if amount to buy is available,
            // otherwise cancel the procedure
            if (productAmount <= 0 || productAmount > product.getStock())
                throw new HibernateException("Stock not available.");//TODO: throw an invalid parameter exception

            // Bind the detail to the product
            d.setPurchases(purchase);

            // Update product stock
            product.setStock(product.getStock() - productAmount);
        }

        // Now the details list is complete and gotta be stored
        new DetailsDaoImpl(session).add(purchaseDetails);

        session.getTransaction().commit();

    } catch (HibernateException e) {
        if (session != null) {
            session.getTransaction().rollback();
            session.close();
        }

        throw new StorageException("Error interno al intentar guardar la compra.");
    }

    return purchaseDetails;
}

From source file:cr.ac.una.fucem.inge.hawac.utils.NewHibernateUtil.java

public void manejaException(HibernateException he) throws HibernateException {
    transac.rollback();//w  ww  . ja  v  a2  s.  c o  m
    throw new HibernateException("Se genero un error con la base de datos" + he.getMessage());

}

From source file:dao.AjusteDao.java

public void manejaexception(HibernateException he) {
    tx.rollback();//from  w  ww . jav a2s .  com
    throw new HibernateException("Ocurrio un error en la capa de acceso a datos");
}

From source file:dao.ApostaDao.java

public List<Aposta> listar() {
    try {//  w  w  w . j  a v  a  2s  .  c  o  m
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Criteria cr = sessao.createCriteria(Aposta.class);
        List<Aposta> resultado = cr.list();
        transacao.commit();
        return resultado;
    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar as apostas. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}

From source file:dao.ApostaDao.java

public List<Aposta> listarApostasPorRodada(Rodada rodada) {
    try {//from   w  w w . j a  v  a2  s .  co  m
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Query consulta = sessao.createQuery("from Aposta a " + " join a.jogo as j" + " join j.rodada as r"
                + " join ra.rodada as r" + " where r = :rod").setParameter("rod", rodada);
        List<Aposta> resultado = consulta.list();
        transacao.commit();

        return resultado;

    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar as apostas. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}

From source file:dao.ApostaDao.java

public List<Aposta> listarApostasPorJogo(Jogo jogo) {
    try {/* w  w  w . j a  v  a 2s  .  co  m*/
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Query consulta = sessao.createQuery("from Aposta a " + " join a.jogo as j" + " where j = :jog")
                .setParameter("jog", jogo);
        List<Aposta> resultado = consulta.list();
        transacao.commit();

        return resultado;

    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar as apostas. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}

From source file:dao.ApostaDao.java

public List<Aposta> listarApostasApostadorPorJogo(Jogo jogo, Apostador apostador) {
    try {//from   w  w w.j a v  a  2  s  . co m
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Query consulta = sessao.createQuery("from Aposta a " + " join a.jogo as j" + " join a.apostador ap"
                + " where j = :jog" + " and ap = :apos").setParameter("jog", jogo)
                .setParameter("apos", apostador);
        List<Aposta> resultado = consulta.list();
        transacao.commit();

        return resultado;

    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar as apostas. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}

From source file:dao.ApostaDao.java

public Apostador buscarApostadorDeUmaAposta(Aposta aposta) {
    try {//from  w  w w .  j a v a2  s.c  o  m
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Query consulta = sessao.createQuery("select a.apostador from Aposta a " + " where a = :apos")
                .setParameter("apos", aposta);
        Apostador resultado = (Apostador) consulta.uniqueResult();
        transacao.commit();
        return resultado;

    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar a Aposta. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}

From source file:dao.ApostaDao.java

public Jogo buscarJogoDeUmaAposta(Aposta aposta) {
    try {//from w  w w .  j  a  v a 2s  .  com
        Session sessao = Hibernate4Util.getSessionFactory();
        Transaction transacao = sessao.beginTransaction();
        Query consulta = sessao.createQuery("select a.jogo from Aposta a " + " where a = :apos")
                .setParameter("apos", aposta);
        Jogo resultado = (Jogo) consulta.uniqueResult();
        transacao.commit();
        return resultado;

    } catch (HibernateException e) {
        System.out.println("No foi possvel selecionar a Aposta. Erro: " + e.getMessage());
        throw new HibernateException(e);
    }
}