Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

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

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:ca.mcgill.cs.swevo.qualyzer.model.PersistenceManagerTest.java

License:Open Source License

/**
 * This is an example of using Hibernate API in a single session.
 * //from ww  w  .  jav a 2s .co  m
 */
// CSOFF:
@Test
public void testHibernateCascade() {
    Session session = null;
    try {
        fManager.initDB(fProject);
        HibernateDBManager dbManager = fActivator.getHibernateDBManagers().get(TEST_PROJECT_NAME);
        session = dbManager.openSession();
        Transaction t = session.beginTransaction();
        Project projectDB = createProject();
        // Test save cascade
        session.saveOrUpdate(projectDB);
        t.commit();
        // The list is still local
        Code tempCode = projectDB.getCodes().get(0);
        assertEquals("b", tempCode.getCodeName());

        // Codes should be persisted on the DB.
        // Note: this is probably a bad query performance-wise.
        Query query = session.createQuery(
                "select count(code) from Code as code, Project as project where project = ? and code in elements(project.codes)")
                .setEntity(0, projectDB);
        assertEquals(2, HibernateUtil.count(query));

        t = session.beginTransaction();
        // Test delete cascade
        session.delete(projectDB);
        t.commit();

        // The codes were deleted from the DB.
        assertEquals(0, HibernateUtil.count(query));
    } catch (HibernateException e) {
        e.printStackTrace();
        fail();
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.util.HibernateUtil.java

License:Open Source License

/**
 * Save an object.//from w  w  w. jav a 2  s. c o m
 * @param manager
 * @param object
 */
public static void quietSave(HibernateDBManager manager, Object object) {
    Transaction t = null;
    Session session = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();
        session.saveOrUpdate(object);
        session.flush();
        t.commit();
    } catch (HibernateException e) {
        quietRollback(t);
    } finally {
        quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.util.HibernateUtil.java

License:Open Source License

/**
 * Save many objects./*from   w w  w . j  a  v a  2  s .  co m*/
 * @param manager
 * @param objects
 */
public static void quietSave(HibernateDBManager manager, Object[] objects) {
    Transaction t = null;
    Session session = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();
        for (Object object : objects) {
            session.saveOrUpdate(object);
        }
        session.flush();
        t.commit();
    } catch (HibernateException e) {
        quietRollback(t);
    } finally {
        quietClose(session);
    }
}

From source file:casenotes.AddNoteController.java

@FXML
void handleAddNoteButton(ActionEvent event) {
    // Open a session to interact with database
    SessionFactory sFactory = HibernateUtilities.getSessionFactory();
    Session theSession = sFactory.openSession();
    theSession.beginTransaction();/*from   w  ww  .j  a v  a  2  s .  c o m*/

    // Create a new Case Note
    CaseNote newNote = new CaseNote();

    // Set the CaseNote's DateTimeCreated to the current Date/Time
    newNote.setDateTimeAdded(LocalDateTime.now());
    // Get the text in the text area and add it to the CaseNote object
    newNote.setNotes(caseNoteTextArea.getText());

    // Add the note to the case
    newNote.setCaseFile(CreateCaseController.getNewCase());

    theSession.saveOrUpdate(newNote);
    theSession.getTransaction().commit();
    theSession.close();

    ((Node) (event.getSource())).getScene().getWindow().hide();
}

From source file:Category.categorySetter.java

public static void updateCategoryById(Integer categoryId, String categoryName, String categoryDescription,
        String categoryType) {/*from  w  w  w  .j a  va2 s. co m*/
    Session session2 = HibernateUtil.getSessionFactory().openSession();
    session2.beginTransaction();
    Category updateCat = new Category();
    updateCat = (Category) session2.get(Category.class, categoryId);
    Hibernate.initialize(updateCat);
    updateCat.setCategoryName(categoryName);
    updateCat.setCategoryDescription(categoryDescription);
    updateCat.setCategoryType(categoryType);
    session2.merge(updateCat);
    session2.saveOrUpdate(updateCat);
    session2.getTransaction().commit();

}

From source file:cd_modelos_dao.EtapasPlantaDAO.java

public void ingresarActualizarEtapasPlanta(EtapasPlanta ep) {
    SessionFactory sf = null;//from  w  w  w.  jav  a2 s .  co  m
    Transaction t = null;
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        s.saveOrUpdate(ep);
    } catch (Exception e) {
    }
}

From source file:cd_modelos_dao.PlantasCompraDAO.java

public void ingresarPlantasCompra(PlantasCompra ep) {
    SessionFactory sf = null;//from   w ww  .j  av  a  2  s.  c o  m
    Transaction t = null;
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        s.saveOrUpdate(ep.getComprasPlanta());
        s.saveOrUpdate(ep);
        t.commit();
        s.close();
    } catch (HibernateException e) {
        e.getMessage();
        System.out.println("plantas compras dao");
        System.out.println(e);
    }
}

From source file:cd_modelos_dao.PlantasVentaDAO.java

public void ingresarPlantasVenta(PlantasVenta ep) {
    SessionFactory sf = null;//from ww  w . j  a  va2s .  c  o m
    Transaction t = null;
    Session s = null;
    try {
        sf = HibernateUtil.getSessionFactory();
        s = sf.openSession();
        t = s.beginTransaction();
        s.saveOrUpdate(ep.getVentasPlanta());
        s.saveOrUpdate(ep);
        t.commit();
        s.close();
    } catch (HibernateException e) {
        e.getMessage();
        System.out.println("plantas ventas dao");
        System.out.println(e);
    }
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

public void save(final E entity) {

    Session currentSession = getCurrentSession();
    currentSession.saveOrUpdate(entity);
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

public void saveAll(final Collection<E> entities) {

    Session currentSession = getCurrentSession();
    for (E entity : entities) {

        currentSession.saveOrUpdate(entity);
    }/*from  w w w.  ja va2s. c  om*/
}