Example usage for org.hibernate Session persist

List of usage examples for org.hibernate Session persist

Introduction

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

Prototype

void persist(Object object);

Source Link

Document

Make a transient instance persistent.

Usage

From source file:org.iti.agrimarket.model.dao.CategoryDAO.java

@Override
public int create(Category category) {
    return (int) transactionTemplate.execute(new TransactionCallback() {
        @Override//from  ww w  .j  a va 2s  .c om
        public Object doInTransaction(TransactionStatus ts) {
            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

                session.persist(category);
                return category.getId();
            } catch (Exception e) {
                e.printStackTrace();
                ts.setRollbackOnly();
            }
            return -1;
        }
    });
}

From source file:org.iti.agrimarket.model.dao.ProductDAO.java

@Override
public int create(Product product) {
    return (int) transactionTemplate.execute(new TransactionCallback() {
        @Override/*from   www .  ja va  2  s  . co  m*/
        public Object doInTransaction(TransactionStatus ts) {
            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

                session.persist(product);
                return product.getId();
            } catch (Exception e) {
                e.printStackTrace();
                ts.setRollbackOnly();
            }
            return -1;
        }
    });
}

From source file:org.iti.agrimarket.model.dao.UserDAO.java

/**
 * @author Amr//w  w  w.j ava  2s .co  m
 * @param user
 * @return user Id
 */
@Override
public int create(User user) {
    return (int) transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus ts) {

            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
                System.out.println("user befor persist in create : " + user.toString());

                session.persist(user);

                System.out.println("user after persist in create : " + user.toString());
                return user.getId();

            } catch (HibernateException e) {
                e.printStackTrace();
                ts.setRollbackOnly();

                return -2; //means DB error 

            } catch (Exception e) {

                e.printStackTrace();
                ts.setRollbackOnly();

                return -1; //means Server error 
            }

        }
    });

}

From source file:org.iti.agrimarket.model.dao.UserRatesUserDAO.java

@Override
public void create(UserRatesUser userRatesUser) {
    transactionTemplate.execute(new TransactionCallback() {
        @Override//  ww  w . j a  va 2  s.  co  m
        public Object doInTransaction(TransactionStatus ts) {
            try {
                Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

                session.persist(userRatesUser);
            } catch (Exception e) {
                e.printStackTrace();
                ts.setRollbackOnly();
            }
            return null;
        }
    });
}

From source file:org.jboss.as.quickstart.hibernate3.controller.MemberRegistration.java

License:Apache License

@Produces
public void register() throws Exception {
    log.info("Registering " + newMember.getName());

    // using Hibernate session(Native API) and JPA entitymanager
    Session session = (Session) em.getDelegate();
    session.persist(newMember);

    try {//  w  w w. j  av  a  2 s. com
        memberEventSrc.fire(newMember);
        initNewMember();
    } catch (Exception e) {
        // Display the reason for the error on the form
        String errorMessage = getRootErrorMessage(e);
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage,
                "Registration unsuccessful");
        facesContext.addMessage(null, facesMessage);
    }
}

From source file:org.jboss.as.test.integration.jpa.hibernate.SFSBHibernateSessionFactory.java

License:Open Source License

public void createEmployee(String name, String address, int id) {
    Employee emp = new Employee();
    emp.setId(id);/*from ww w  .  j  a  va  2s  . c  o m*/
    emp.setAddress(address);
    emp.setName(name);
    try {
        Session session = sessionFactory.openSession();
        session.persist(emp);
        session.flush();
        session.close();
    } catch (Exception e) {
        throw new RuntimeException("transactional failure while persisting employee entity", e);
    }
}

From source file:org.kaaproject.kaa.server.common.dao.impl.sql.HibernateAbstractDao.java

License:Apache License

@Override
public T persist(T object) {
    LOG.debug("Persisting {} entity {}", getEntityClass(), object);
    Session session = getSession();
    session.persist(object);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Persisting result: {}", object);
    } else {/*from ww  w. ja  v a 2  s . co  m*/
        LOG.debug("Persisting result: {}", object != null);
    }
    return object;
}

From source file:org.kalypso.model.wspm.pdb.wspm.CheckinStatePdbOperation.java

License:Open Source License

private State saveOrUpdateState(final Session session) {
    final State state = m_data.getState();
    state.setEditingUser(m_data.getUsername());
    state.setEditingDate(new Date());

    /* if state already contains cross sections or documents: delete everything */
    // do not delete existing events, they do not get uploaded via this mechanism
    final Set<CrossSection> crossSections = state.getCrossSections();
    for (final CrossSection crossSection : crossSections)
        session.delete(crossSection);/*from w w w. ja v  a 2  s  . c om*/
    crossSections.clear();

    final Set<Document> documents = state.getDocuments();
    for (final Document document : documents)
        session.delete(document);
    documents.clear();

    if (session == null)
        return state;

    /* add state to db if it is new */
    if (!session.contains(state))
        session.save(state);
    else
        session.persist(state);

    // FIXME: necessary to prevent problems with same cross sectionsre-checked in; else db whines about duplicate name
    // check if this breaks the transaction
    session.flush();

    // REMARK: nothing to do if already attached to session, in this case all changes are persited when transaction is closed.

    return state;
}

From source file:org.lgb.service.FileService.java

public static Version upload(UUID id, InputStream uploadedInputStream) throws IOException {
    Content content = ContentService.store(uploadedInputStream);
    Session session = sessionFactory.getCurrentSession();
    Transaction trans = session.beginTransaction();
    File file = (File) session.load(File.class, id);
    //Only create a new version if the current version content is different
    if (file.getLastestVersion() == null || file.getLastestVersion().getContent().getId() != content.getId()) {
        Version version = new Version(file, content);
        file.setModified(new Date());
        file.setLatestVersion(version);// w  ww  .  j av a2s.  c o  m
        session.persist(file);
        session.persist(version);
        trans.commit();
        return version;
    }
    trans.commit();

    return file.getLastestVersion();
}

From source file:org.opengoss.dao.hibernate.DataAccessor.java

License:Apache License

public void persist(final Object entity) throws DaoException {
    execute(new IAccessorCallback() {
        public Object call(Session session) throws HibernateException {
            session.persist(entity);
            return null;
        }/*from  w  ww .  ja  va2 s. c  om*/
    });
}