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.sakaiproject.assignment.impl.persistence.AssignmentRepositoryImpl.java

License:Educational Community License

@Override
@Transactional// w ww .j  a v  a 2s.c  o  m
public AssignmentSubmission newSubmission(String assignmentId, Optional<String> groupId,
        Optional<Set<AssignmentSubmissionSubmitter>> submitters, Optional<Set<String>> feedbackAttachments,
        Optional<Set<String>> submittedAttachments, Optional<Map<String, String>> properties) {
    Assignment assignment = findAssignment(assignmentId);
    if (assignment != null) {
        Session session = sessionFactory.getCurrentSession();
        // Since this transaction is going to add a submission to the assignment we lock the assignment
        // the lock is freed once transaction is committed or rolled back
        session.buildLockRequest(LockOptions.UPGRADE).setLockMode(LockMode.PESSIMISTIC_WRITE).lock(assignment);

        AssignmentSubmission submission = new AssignmentSubmission();
        submission.setDateCreated(Instant.now());
        submitters.ifPresent(submission::setSubmitters);
        submitters.ifPresent(s -> s.forEach(submitter -> submitter.setSubmission(submission)));
        feedbackAttachments.ifPresent(submission::setFeedbackAttachments);
        submittedAttachments.ifPresent(submission::setAttachments);
        properties.ifPresent(submission::setProperties);
        if (assignment.getIsGroup()) {
            groupId.ifPresent(submission::setGroupId);
        }

        submission.setAssignment(assignment);
        assignment.getSubmissions().add(submission);

        session.persist(assignment);
        return submission;
    }
    return null;
}

From source file:org.sindicato.controllers.AbstractController.java

/**
 * Persist obj in DataBase//w ww .  j a v  a 2 s .  c  o m
 * @param obj
 * @return true if obj was added suyccessfuly- False in otherwise.
 */
public boolean add(T obj) {
    sf = HibernateUtil.getSessionFactory();
    boolean resp = false;
    try {
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        s.persist(obj);
        tx.commit();
        s.close();
        resp = true;
    } catch (Exception e) {
        Log.log(Logger.Level.FATAL, e.getMessage());
        e.printStackTrace();
    }
    return resp;
}

From source file:org.socraticgrid.hl7.services.orders.functional.TestHibernate.java

License:Apache License

public static void main(String[] args) {
    // creating configuration object 
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file 
    //creating session factory object 
    SessionFactory factory = cfg.buildSessionFactory();
    //creating session object 
    Session session = factory.openSession();
    //creating transaction object
    Transaction t = session.beginTransaction();
    Quantity quantity = new Quantity();
    quantity.setCode("KG");
    quantity.setUnits("KG");
    quantity.setValue("500");
    session.persist(quantity);
    t.commit();//  w  ww .ja  va 2s  .com
    session.close();
}

From source file:org.springframework.orm.hibernate3.HibernateTemplate.java

License:Apache License

@Override
public void persist(final Object entity) throws DataAccessException {
    executeWithNativeSession(new HibernateCallback<Object>() {
        @Override//  www .  java2 s.c o  m
        public Object doInHibernate(Session session) throws HibernateException {
            checkWriteOperationAllowed(session);
            session.persist(entity);
            return null;
        }
    });
}

From source file:org.springframework.orm.hibernate3.StatelessHibernateTemplate.java

License:Apache License

public void persist(final Object entity) throws DataAccessException {
    execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            checkWriteOperationAllowed(session);
            session.persist(entity);
            return null;
        }// w w  w  . j  a v a  2 s . com
    }, true);
}

From source file:org.storezilla.product.dao.ProductImpl.java

@Override
public void addProduct(Product product) {
    Session session = sessionFactory.getCurrentSession();
    session.persist(product);
}

From source file:org.tec.webapp.orm.dao.impl.UserDaoImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from ww  w. ja va  2  s  . c  o  m*/
 */
@Override()
public void insert(UserBean user) {
    Session session = mSessionFactory.getCurrentSession();

    if (mLogger.isDebugEnabled()) {
        mLogger.debug("inserting " + user);
    }

    session.persist(user);
}

From source file:org.transitime.db.structs.ActiveRevisions.java

License:Open Source License

/**
 * Gets the ActiveRevisions object using the passed in database session.
 * //  w  ww  .  j  a va2s  . c  o m
 * @param session
 * @return the ActiveRevisions
 * @throws HibernateException
 */
public static ActiveRevisions get(Session session) throws HibernateException {
    // There should only be a single object so don't need a WHERE clause
    String hql = "FROM ActiveRevisions";
    Query query = session.createQuery(hql);
    ActiveRevisions activeRevisions = null;
    try {
        activeRevisions = (ActiveRevisions) query.uniqueResult();
    } catch (Exception e) {
        System.err
                .println("Exception when reading ActiveRevisions object " + "from database so will create it");
    } finally {
        // If couldn't read from db use default values and write the
        // object to the database.
        if (activeRevisions == null) {
            activeRevisions = new ActiveRevisions();
            session.persist(activeRevisions);
        }
    }

    // Return the object
    return activeRevisions;
}

From source file:org.uclab.mm.kcl.edkat.dao.WellnessConceptsModelDAOImpl.java

License:Apache License

/**
   * This function is the implementation for add new Wellness Concept
 * @param objWellnessConceptsModel//from w w  w.j a va 2 s.  c o  m
*/
public void addWellnessConcept(WellnessConceptsModel objWellnessConceptsModel) {
    try {
        Session session = this.sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.persist(objWellnessConceptsModel);
        tx.commit();
        session.close();
        logger.info("Wellness Concepts Model saved successfully, Wellness Concepts Model Details="
                + objWellnessConceptsModel);
    } catch (Exception ex) {
        logger.info("Error occurred in wellness concept saving" + ex.getMessage());
    }

}

From source file:org.unicode4all.lorquotes.dao.QuoteDAOImplementation.java

License:Apache License

@Override
public void addQuote(Quote q) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(q);
    logger.info("Quote saved successfully, Quote Details=" + q);
}