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.asociate.dao.UsuarioDAO.java

/**
 *
 * @param idUsuario//from  w w  w .  ja  va 2  s . com
 */
public void guardar(Usuario idUsuario) {
    Session sesion = HibernateUtil.getSessionFactory().openSession();

    try {
        sesion.save(idUsuario);

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        sesion.flush();
        sesion.close();
    }
}

From source file:com.asociate.dao.UsuarioDAO.java

/**
 *
 * @param id//  w  w w  .ja  va 2s  .  c  o  m
 * @return
 */
public static Usuario getPorID(Long id) {
    Session sesion = HibernateUtil.getSessionFactory().openSession();
    Usuario salida = null;
    try {
        salida = (Usuario) sesion.get(Usuario.class, id);

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        sesion.flush();
        sesion.close();
    }
    return salida;
}

From source file:com.asociate.dao.UsuarioDAO.java

/**
 *
 * @param amigos//  w  w w  .  j  a  v  a 2  s . co m
 * @return
 */
public List<Usuario> getAmigosDeLista(List<Amistad> amigos) {
    List<Usuario> salida = new ArrayList();
    Session sesion = HibernateUtil.getSessionFactory().openSession();
    StringBuffer sb = new StringBuffer();
    for (Amistad am : amigos) {
        sb.append(" ");
        sb.append(am.getIdAmigo());
        sb.append(" ,");
    }
    sb.delete(sb.lastIndexOf(",") - 1, sb.length());
    try {
        Query qu = sesion.createQuery("Select U from Usuario U where U.persona.idPersona in(:list)");
        qu.setParameter("list", sb.toString());
        salida = qu.list();

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        sesion.flush();
        sesion.close();
    }
    return salida;
}

From source file:com.autentia.intra.util.HibernateUtil.java

License:Open Source License

public static void closeSession(Session s) throws HibernateException {
    if (s != null) {
        s.flush();
        s.close();// w  ww  .j  av a2s.  c o m
    }
}

From source file:com.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

/**
 * Persist a new account and a user associated with the account.
 *
 * @param account the new {@code Account} object that holds the account's attributes.
 * @param user the new {@code User} object that holds the user's attributes to be created along with the account.
 *            May be set to {@code null} to create an account without a user.
 * @return An array of the newly persisted {@code Account} and {@code User} objects.
 * @throws DuplicateAccountException if the specified account already exists.
 * @throws DuplicateUserException if the specified user already exists.
 * @throws PasswordPolicyException if the passphrase set for the user does not fulfill the password policy
 *             requirements.// w w w .ja  v a 2  s.  c o  m
 * @throws AccountConstraintException if the account home folder is not valid.
 */
private Account persistAccount(Account account)
        throws DuplicateAccountException, PasswordPolicyException, AccountConstraintException {

    if (account.getId() != null) {
        throw new IllegalArgumentException("Instantiated from Template");
    }

    Session session = mSessionFactoryManager.getSessionFactory().openSession();

    try {

        ConstraintValidationUtil.validateNewAccount(session, (AccountBean) account);

        Transaction tx = session.beginTransaction();

        Account persistedAccount = account;
        try {
            if (account.getId() == null) {
                // If object does not have an id, persist a copy so if exception is thrown original object is
                // unchanged.
                persistedAccount = ((AccountBean) account).copy();
                session.save(persistedAccount);
            } else {
                session.replicate(persistedAccount, ReplicationMode.OVERWRITE);
            }
        } catch (ConstraintViolationException ex) {
            // Assume this is a uniqueness violation on the name since that is the only constraint in this bean.
            throw new DuplicateAccountExceptionImpl(account, "Duplicate account: " + account.getLoginname(),
                    ex);
        } catch (HibernateException e) {
            final String message = "Database error creating account";
            throw new RuntimeException(message, e);
        }
        session.flush();

        tx.commit();

        if (sLogger.isDebugEnabled()) {
            sLogger.debug("76568:createAccount : " + account);
        }

        return persistedAccount;
    } finally {
        SessionManagerFactory.closeSession(session);
    }
}

From source file:com.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

@Override
public void updateAccount(Account account) throws NoSuchAccountException, DuplicateAccountException,
        PasswordPolicyException, AccountConstraintException {

    Session session = mSessionFactoryManager.getSessionFactory().openSession();

    try {// www . ja  va  2s. c  om

        ConstraintValidationUtil.validateUpdateAccount(session, (AccountBean) account);

        Transaction tx = session.beginTransaction();

        try {
            session.replicate(account, ReplicationMode.OVERWRITE);
            session.flush();

        } catch (UnresolvableObjectException ex) {
            throw new NoSuchAccountException(account.getLoginname(), ex);
        } catch (ConstraintViolationException ex) {
            throw new DuplicateAccountExceptionImpl(account, "Duplicate account: " + account.getLoginname(),
                    ex);
        }

        tx.commit();

        if (sLogger.isDebugEnabled()) {
            sLogger.debug("76568: updateAccount updated: " + account, new Throwable("trace caller"));
        }

    } finally {
        SessionManagerFactory.closeSession(session);
    }

}

From source file:com.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

private void updategetAddressBookEntry(Session session, AddressBookEntryBean entry)
        throws NoSuchAddressBookEntryException, DuplicateAddressBookEntryException {

    try {// ww  w  .j  a v a2 s .c om
        AddressBookEntryBean addressBookEntryBean = (AddressBookEntryBean) entry;
        AccountBean account = (AccountBean) getAccount(session, addressBookEntryBean.getAccountId());

        if (addressBookEntryBean.getId() == null) {
            throw new NoSuchAddressBookEntryException("AddressBookContact does not exist yet");
        }

        for (AddressBookEntryBean existingContact : account.getAddressBookEntries()) {
            if (((AddressBookEntryBean) entry).getId().equals(existingContact.getId())) {
                account.removeAddressBookContact(existingContact);
                session.delete(existingContact);
                session.flush();
                break;
            }
        }
        addAddressBookEntry(session, addressBookEntryBean.getAccountId(), addressBookEntryBean);

    } catch (ConstraintViolationException ex) {
        throw new DuplicateAddressBookEntryException("Duplicate address book contact: " + entry.getEmail(), ex);
    } catch (UnresolvableObjectException ex) {
        throw new NoSuchAddressBookEntryException("Id: " + entry.getId().toString());
    } catch (NoSuchAccountException e) {
        throw new NoSuchAddressBookEntryException("Assigned account does not exist", e);
    }

    if (sLogger.isDebugEnabled()) {
        sLogger.debug("76568: updateAddressBookContact updated: " + entry, new Throwable("trace caller"));
    }
}

From source file:com.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

private AddressBookEntry addAddressBookEntry(Session session, Account.Id accountId, AddressBookEntry entry)
        throws DuplicateAddressBookEntryException, NoSuchAccountException {

    // Create the association between the contact and account.
    AccountBean account = (AccountBean) getAccount(session, accountId);
    ((AddressBookEntryBean) entry).setAccountId(accountId);

    // If object does not have an id, persist a clone so if exception is thrown original object is unchanged.
    AddressBookEntry persistedContact = (entry.getId() == null) ? ((AddressBookEntryBean) entry).clone()
            : entry;/*from www  .  j  a  v a2  s .c  o  m*/
    account.addAddressBookContact((AddressBookEntryBean) persistedContact);

    try {
        session.save(persistedContact);
        session.flush();
    } catch (ConstraintViolationException ex) {
        throw new DuplicateAddressBookEntryException("Duplicate address book entry: " + entry.getEmail(), ex);
    }

    return persistedContact;
}

From source file:com.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

@Override
public void deleteAddressBookEntry(String addressBookEntryId) throws NoSuchAddressBookEntryException {

    Session session = mSessionFactoryManager.getSessionFactory().openSession();

    try {//  w ww. j av a 2  s.  c om
        Transaction tx = session.beginTransaction();
        AddressBookEntryBean addressBookEntry = (AddressBookEntryBean) session.get(AddressBookEntryBean.class,
                addressBookEntryId);
        if (addressBookEntry == null) {
            throw new NoSuchAddressBookEntryException("Id: " + addressBookEntryId.toString());
        }

        AccountBean account = null;
        try {
            account = (AccountBean) getAccount(session, addressBookEntry.getAccountId());
        } catch (NoSuchAccountException e) {
            // ignore, no account associated to this address book contact.
        }
        if (account != null) {
            account.removeAddressBookContact(addressBookEntry);
        }

        session.delete(addressBookEntry);
        session.flush();
        tx.commit();

        if (sLogger.isDebugEnabled()) {
            sLogger.debug("76568: deleteAddressBookContact deleted: " + addressBookEntry,
                    new Throwable("trace caller"));
        }

    } finally {
        SessionManagerFactory.closeSession(session);
    }

}

From source file:com.bacic5i5j.framework.database.HibernateAccess.java

License:Open Source License

@Override
public PK save(T entity) {
    Session session = sessionFactory.currentSession();
    Serializable id = session.save(entity);
    session.flush();
    sessionFactory.closeSession();//from   www.  j  a va2  s.  co  m

    if (id == null) {
        return null;
    }
    return (PK) id;
}