Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:Category.categorySetter.java

public static void updateCategoryById(Integer categoryId, String categoryName, String categoryDescription,
        String categoryType) {//w w  w.  jav  a  2 s  .c  o 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:ch.algotrader.dao.AbstractDao.java

License:Open Source License

public E persist(final E entity) {

    Session currentSession = getCurrentSession();
    if (entity.getId() == 0) {
        currentSession.save(entity);//ww w  . j  a  v a  2  s .c o  m
    } else if (!currentSession.contains(entity)) {
        return this.entityClass.cast(currentSession.merge(entity));
    }
    return entity;
}

From source file:cn.newtouch.model.TeacherTest.java

License:Open Source License

@Test
public void testUpdate6() {

    Session session = HibernateUtil.getSession();
    session.beginTransaction();//from  w  w w .  j  a  v a  2  s.c  o m
    Student s = (Student) session.get(Student.class, 1L);
    s.setName("zhangsan6");
    session.getTransaction().commit();

    s.setName("z4");

    Session session2 = HibernateUtil.getSession();
    session2.beginTransaction();
    // ?
    session2.merge(s);
    session2.getTransaction().commit();
}

From source file:com.algoTrader.util.HibernateUtil.java

License:Open Source License

public static Object merge(SessionFactory sessionFactory, Object target) {

    Session session = sessionFactory.getCurrentSession();

    return session.merge(target);
}

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

License:Apache License

public Boolean save() {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {/*from ww  w .  j  a v a 2  s.  c  o m*/
        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 {// www .jav a2 s  .  c om
        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 {/* w  w  w  .j  av  a  2s. co  m*/
        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  v a 2 s. c  o  m
        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;
}

From source file:com.ardikapras.controller.supplier.SupplierFormCtrl.java

License:Apache License

public Boolean save() {
    boolean closeAfter = false;
    boolean isSuccess = false;
    try {// w ww  .  ja  va  2  s.co  m
        closeAfter = HibernateHelper.beginTx();
        Session session = HibernateHelper.getSession();
        supplier.setIsActive('1');
        if (getIsNew()) {
            supplier.setId(CommonUtil.generateId(CommonID.Supplier.getVal()));
            supplier.setUserCreated(UserBean.getInstance().getIdUser());
            supplier.setTimeCreated(new Date());
            session.save(supplier);
        } else {
            supplier.setUserUpdated(UserBean.getInstance().getIdUser());
            supplier.setTimeUpdated(new Date());
            session.merge(supplier);
        }
        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.supplier.SupplierFormCtrl.java

License:Apache License

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