Example usage for org.hibernate Session flush

List of usage examples for org.hibernate Session flush

Introduction

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

Prototype

void flush() throws HibernateException;

Source Link

Document

Force this session to flush.

Usage

From source file:com.alejandro.modelo.ModeloImagen.java

public static void edit(Imagen p) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from w w  w. j  a v  a2 s . c o m

    session.update(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.alejandro.modelo.ModeloInmueble.java

public static void delete(String id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*w  ww .j  a v a 2s  .  c om*/

    Inmueble p = (Inmueble) session.load(Inmueble.class, Integer.parseInt(id));
    session.delete(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.alejandro.modelo.ModeloInmueble.java

public static void insert(Inmueble p) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from w ww.  j av  a 2s .  com

    session.save(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.alejandro.modelo.ModeloInmueble.java

public static Integer insertID(Inmueble in) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from  w  w w  . j  a v a2s  .c  o m
    Integer id = (Integer) session.save(in);
    session.getTransaction().commit();
    session.flush();
    session.close();
    return id;
}

From source file:com.alejandro.modelo.ModeloInmueble.java

public static void edit(Inmueble p) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from   ww w  . jav a2s  . co  m

    session.update(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.amalto.core.storage.hibernate.HibernateStorage.java

License:Open Source License

@Override
public void update(Iterable<DataRecord> records) {
    assertPrepared();//from   w  w w. ja v a  2s  . co m
    Session session = this.getCurrentSession();
    try {
        storageClassLoader.bind(Thread.currentThread());
        DataRecordConverter<Object> converter = new ObjectDataRecordConverter(storageClassLoader, session);
        for (DataRecord currentDataRecord : records) {
            TypeMapping mapping = mappingRepository.getMappingFromUser(currentDataRecord.getType());
            Wrapper o = (Wrapper) converter.convert(currentDataRecord, mapping);
            if (session.contains(o) && session.isReadOnly(o)) { // A read only instance for an update?
                // Session#setReadOnly(...) does not always work as expected (especially in case of compound keys
                // see TMDM-7014).
                session.evict(o);
                o = (Wrapper) converter.convert(currentDataRecord, mapping);
            }
            DataRecordMetadata recordMetadata = currentDataRecord.getRecordMetadata();
            Map<String, String> recordProperties = recordMetadata.getRecordProperties();
            if (!ObjectUtils.equals(recordMetadata.getTaskId(), o.taskId())) {
                o.taskId(recordMetadata.getTaskId());
            }
            for (Map.Entry<String, String> currentProperty : recordProperties.entrySet()) {
                String key = currentProperty.getKey();
                String value = currentProperty.getValue();
                ComplexTypeMetadata database = mapping.getDatabase();
                if (database.hasField(key)) {
                    Object convertedValue = StorageMetadataUtils.convert(value, database.getField(key));
                    if (!ObjectUtils.equals(convertedValue, o.get(key))) {
                        o.set(key, convertedValue);
                    }
                } else {
                    throw new IllegalArgumentException("Can not store value '" + key //$NON-NLS-1$
                            + "' because there is no database field '" + key + "' in type '" + mapping.getName() //$NON-NLS-1$ //$NON-NLS-2$
                            + "' (storage is '" + toString() + "')"); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            session.saveOrUpdate(o);
            if (FLUSH_ON_LOAD && session.getStatistics().getEntityCount() % batchSize == 0) {
                // Periodically flush objects to avoid using too much memory.
                session.flush();
            }
        }
    } catch (ConstraintViolationException e) {
        throw new com.amalto.core.storage.exception.ConstraintViolationException(e);
    } catch (PropertyValueException e) {
        throw new RuntimeException("Invalid value in record to update.", e); //$NON-NLS-1$
    } catch (NonUniqueObjectException e) {
        throw new RuntimeException("Attempted to update multiple times same record within same transaction.", //$NON-NLS-1$
                e);
    } catch (Exception e) {
        throw new RuntimeException("Exception occurred during update.", e); //$NON-NLS-1$
    } finally {
        this.releaseSession();
        storageClassLoader.unbind(Thread.currentThread());
    }
}

From source file:com.ardikapras.controller.customer.CustomerFormCtrl.java

License:Apache License

public Boolean save() {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {//from  w  ww . j  a  va2 s  .c om
        closeAfter = HibernateHelper.beginTx();
        Session session = HibernateHelper.getSession();
        customer.setIsActive('1');
        if (getIsNew()) {
            customer.setId(CommonUtil.generateId(CommonID.Customer.getVal()));
            customer.setUserCreated(UserBean.getInstance().getIdUser());
            customer.setTimeCreated(new Date());
            session.save(customer);
        } else {
            customer.setUserUpdated(UserBean.getInstance().getIdUser());
            customer.setTimeUpdated(new Date());
            session.merge(customer);
        }
        session.flush();
        isSuccess = true;
        HibernateHelper.commitTx(closeAfter);
    } catch (Exception e) {
        HibernateHelper.rollbackTx(closeAfter);
        JOptionPane.showMessageDialog(null, e);
    }
    return isSuccess;
}

From source file:com.ardikapras.controller.customer.CustomerFormCtrl.java

License:Apache License

public Boolean delete(String idCustomer) {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {/*from   ww  w  .j  ava  2s  .co m*/
        closeAfter = HibernateHelper.beginTx();
        Session session = HibernateHelper.getSession();
        if (customer.getIsActive() == '1') {
            customer.setIsActive('0');
        } else {
            customer.setIsActive('1');
        }
        customer.setUserUpdated(UserBean.getInstance().getIdUser());
        customer.setTimeUpdated(new Date());
        session.merge(customer);
        session.flush();
        isSuccess = true;
        HibernateHelper.commitTx(closeAfter);
    } catch (Exception e) {
        HibernateHelper.rollbackTx(closeAfter);
        JOptionPane.showMessageDialog(null, e);
    }
    return isSuccess;
}

From source file:com.ardikapras.controller.product.ProductFormCtrl.java

License:Apache License

public Boolean save() {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {/*from  w w  w  .j a  v  a 2s  .  com*/
        closeAfter = HibernateHelper.beginTx();
        Session session = HibernateHelper.getSession();
        product.setIsActive('1');
        if (getIsNew()) {
            product.setId(CommonUtil.generateId(CommonID.Supplier.getVal()));
            product.setUserCreated(UserBean.getInstance().getIdUser());
            product.setTimeCreated(new Date());
            session.save(product);
        } else {
            product.setUserUpdated(UserBean.getInstance().getIdUser());
            product.setTimeUpdated(new Date());
            session.merge(product);
        }
        session.flush();
        isSuccess = true;
        HibernateHelper.commitTx(closeAfter);
    } catch (Exception e) {
        HibernateHelper.rollbackTx(closeAfter);
        JOptionPane.showMessageDialog(null, e);
    }
    return isSuccess;
}

From source file:com.ardikapras.controller.product.ProductFormCtrl.java

License:Apache License

public Boolean delete(String idProduct) {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {/*from   w  w w .  j  a  va 2s . c  om*/
        closeAfter = HibernateHelper.beginTx();
        Session session = HibernateHelper.getSession();
        if (product.getIsActive() == '1') {
            product.setIsActive('0');
        } else {
            product.setIsActive('1');
        }
        product.setUserUpdated(UserBean.getInstance().getIdUser());
        product.setTimeUpdated(new Date());
        session.merge(product);
        session.flush();
        isSuccess = true;
        HibernateHelper.commitTx(closeAfter);
    } catch (Exception e) {
        HibernateHelper.rollbackTx(closeAfter);
        JOptionPane.showMessageDialog(null, e);
    }
    return isSuccess;
}