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:alma.hibernate.util.StringEnumUserType.java

License:Open Source License

public void setParameterValues(Properties parameters) {
    String enumClassName = parameters.getProperty("enumClassName");
    try {/*from ww  w  .ja  va 2 s. c  o m*/
        enumClass = ReflectHelper.classForName(enumClassName);
        try {
            enumClass.getMethod("valueOfForEnum", String.class);
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
            throw new HibernateException("Class '" + enumClass.getCanonicalName()
                    + "' does not implement the valueOfForEnum(String), cannot proceed");
        }
    } catch (ClassNotFoundException cnfe) {
        throw new HibernateException("Enum class not found", cnfe);
    }
}

From source file:alma.hibernate.util.StringEnumUserType.java

License:Open Source License

public Object fromXMLString(String xmlValue) {
    try {//from  w ww  .  j  av  a 2  s. com
        return enumClass.getMethod("valueOfForEnum", String.class).invoke(enumClass, xmlValue);
    } catch (Exception e) {
        e.printStackTrace();
        throw new HibernateException(
                "Cannot invoke valueOfForEnum(String) in class '" + enumClass.getCanonicalName() + "'");
    }
}

From source file:alma.hibernate.util.StringEnumUserType.java

License:Open Source License

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
    String name = rs.getString(names[0]);
    try {/*from w  w  w  . j  a v a2s .c om*/
        return rs.wasNull() ? null
                : enumClass.getMethod("valueOfForEnum", String.class).invoke(enumClass, name);
    } catch (Exception e) {
        throw new HibernateException(
                "Cannot invoke valueOfForEnum(String) in class '" + enumClass.getCanonicalName() + "'");
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public void create(T entity) {
    session = getSession();//  w  w w  .  j  av  a  2s .c o m
    try {
        transaction = session.beginTransaction();
        session.save(entity);
        transaction.commit();
    } catch (HibernateException hex) {
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public void update(T entity) {
    session = getSession();/*from  w  w  w  . j a  va 2  s  .c  om*/
    try {
        transaction = session.beginTransaction();
        session.saveOrUpdate(entity);
        transaction.commit();
    } catch (HibernateException hex) {
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public void delete(T entity) {
    session = getSession();//from   w  w w . j  a  va 2 s.c  om
    try {
        transaction = session.beginTransaction();
        session.delete(entity);
        transaction.commit();
    } catch (HibernateException hex) {
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public List<T> getAll() {
    List<T> list;// w ww.j av  a  2 s  . c  o m
    session = getSession();
    try {
        list = session.createQuery("FROM " + getTTypeName()).list();
        return list;
    } catch (HibernateException hex) {
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public List<T> execNamedQuery(String namedQuery) {
    List<T> list;/*from   ww w  . ja  va  2 s. c om*/
    session = getSession();
    try {
        transaction = session.beginTransaction();
        list = session.getNamedQuery(namedQuery).list();
        transaction.commit();
        return list;
    } catch (HibernateException hex) {
        transaction.rollback();
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:alpha.entite.dao.generiq.GeneriqDAO.java

@Override
public List<T> execFreeHQLQuery(String freeQuery) {
    List<T> list;/*from w w  w . j a  va  2s .  co  m*/
    session = getSession();
    try {
        transaction = session.beginTransaction();
        list = session.createQuery(freeQuery).list();
        transaction.commit();
        return list;
    } catch (HibernateException hex) {
        transaction.rollback();
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:beans.Detalles.java

public String purchaseProducts(CarritoCompra cart, int userLoggedID)
        throws StorageException, InvalidParameterException {
    generateDetails(cart);/*from ww  w  .  j a v a  2 s .  co m*/

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

    Integer productAmount, productId;
    // Get the user who made the purchase
    Users user = UsersController.getUser(userLoggedID);
    // Generate a new purchase
    Purchases purchase = new Purchases(user, false);
    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 : getDetalles()) {
            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(getDetalles());
        session.getTransaction().commit();
        setSuccess(true);
    } catch (HibernateException e) {
        if (session != null) {
            session.getTransaction().rollback();
            session.close();
        }
        throw new StorageException("Error interno al intentar guardar la compra.");
    }
    return "detalles";
}