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:DataBaseCredentialHandler.java

@Override
public void zapiszWynik(Long EmployeeID, Map mapa) {
    Session session = factory.openSession();
    Transaction tx = null;/*ww  w  . j  av a 2s . c  om*/
    try {

        tx = session.beginTransaction();
        // session.delete(user.getMapaWynikow());
        User user = session.get(User.class, EmployeeID);

        user.setMapaWynikow(mapa);
        session.merge(user);
        tx.commit();

    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

}

From source file:TestPurchase.java

@Test
public void testSave() {
    Purchase entity = new Purchase();
    entity.setId(0);/*from w w  w .jav  a2 s  .  co m*/
    entity.setStatus(PurchaseStatus.W);

    PurchaseItemService sitem = new PurchaseItemService();
    sitem.setId(0);
    Service service = new Service();
    service.setId(1);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.I);
    entity.getServices().add(sitem);
    sitem = new PurchaseItemService();
    sitem.setId(0);
    service = new Service();
    service.setId(2);
    sitem.setService(service);
    sitem.setFlagVat(FlagVat.E);
    entity.getServices().add(sitem);
    PurchaseItemProduct pitem = new PurchaseItemProduct();
    pitem.setId(0);
    Product p = new Product();
    p.setId(1);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);

    pitem = new PurchaseItemProduct();
    pitem.setId(0);
    p = new Product();
    p.setId(2);
    pitem.setProduct(p);
    pitem.setFlagVat(FlagVat.I);
    entity.getProducts().add(pitem);
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Purchase data = (Purchase) session.get(Purchase.class, entity.getId());
    Purchase result = (Purchase) session.merge(entity);
    tran.commit();
}

From source file:TestEmployee.java

@Test
public void saveEmployee() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Employee entity = new Employee();
    entity.setId(0);//from  ww w  . j av a2s  .  c  o m
    Education item = new Education();
    item.setId(0);
    item.setEmployeeId(null);
    item.setEducationLevel(null);
    entity.getEducations().add(item);
    Employee data = (Employee) session.get(Employee.class, entity.getId());
    Employee result = (Employee) session.merge(entity);
    System.out.println(result.getId());
    tran.commit();

    //Employee data =(Employee) session.get(Employee.class, 0);
    //Employee employee = (Employee) session.get(Employee.class, 3);
    //System.out.println(employee.getId());

}

From source file:TestCustomer.java

@Test
public void saveCustomer() {
    Configuration config = new Configuration();
    SessionFactory factory = config.configure("hibernate.cfg.xml").buildSessionFactory();
    Session session = factory.openSession();
    Transaction tran = session.beginTransaction();
    Customer entity = new Customer();
    entity.setId(0);/*from w  ww  .  j a  v a  2 s.  c o  m*/
    entity.setCustomerType(CustomerType.P);
    Customer data = (Customer) session.get(Customer.class, entity.getId());
    Customer result = (Customer) session.merge(entity);
    tran.commit();
    System.out.print(result.getId());
}

From source file:Activity.activitySetter.java

public static void updateActivityById(Integer activityId, String activityName, Category category) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from   w ww  .ja v  a 2 s. c  o  m
    Activity updateAct = new Activity();
    updateAct = (Activity) session.get(Activity.class, activityId);
    Hibernate.initialize(updateAct);
    updateAct.setActivityName(activityName);
    updateAct.setCategory(category);
    session.merge(updateAct);
    session.saveOrUpdate(updateAct);
    session.getTransaction().commit();

}

From source file:Activity.activitySetter.java

public static void updateActivityReportById(Integer activityReportId, Activity activity,
        String activityDescription, Timestamp activityStartTime, Timestamp activityEndTime) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/* w ww.j  ava2 s  .  c  om*/
    ActivityReport updateActRep = new ActivityReport();
    updateActRep = (ActivityReport) session.get(ActivityReport.class, activityReportId);
    Hibernate.initialize(updateActRep);
    updateActRep.setActivity(activity);
    updateActRep.setActivityDescription(activityDescription);
    updateActRep.setActivityStartTime(activityStartTime);
    updateActRep.setActivityEndTime(activityEndTime);
    session.merge(updateActRep);
    session.saveOrUpdate(updateActRep);
    session.getTransaction().commit();

}

From source file:appointment.data.ObjectFactory.java

public Object updateDataResource(Object resource) {
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Object mergedOne = session.merge(resource);
    session.saveOrUpdate(mergedOne);//w  w  w .j a  v  a 2  s. c om
    tx.commit();

    return mergedOne;
}

From source file:au.edu.anu.metadatastores.harvester.Harvest.java

License:Open Source License

/**
 * Update the location information//from w  w  w .ja va  2s .  c  o  m
 * 
 * @param location The location information to update
 */
public void updateLocation(Location location) {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();

        session.merge(location);

        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.datacommons.DataCommonsService.java

License:Open Source License

/**
 * Process records that have the deleted status from oai-pmh
 * //from   w  ww.  ja  va  2  s  .  c o m
 * @param content The harvested record to process
 */
private void processDeleted(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();

        Query query = session
                .createQuery("FROM DataCommonsItem WHERE extSystem = :extSystem AND extId = :extId");
        query.setParameter("extSystem", extSystem);
        query.setParameter("extId", content.getIdentifier());

        DataCommonsItem item = (DataCommonsItem) query.uniqueResult();

        if (item != null) {
            item.setDeleted(Boolean.TRUE);
            session.merge(item);
        } else {
            LOGGER.debug("No record to be deleted: {}", content.getIdentifier());
        }
        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.datacommons.DataCommonsService.java

License:Open Source License

/**
 * Process a record that has not been deleted
 * /*from   w  w w.  ja v  a  2 s .c  om*/
 * @param content The harvested record to process
 */
private void processRecord(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.enableFilter("attributes");

        Query query = session
                .createQuery("FROM DataCommonsItem WHERE extSystem = :extSystem AND extId = :extId");
        query.setParameter("extSystem", extSystem);
        query.setParameter("extId", content.getIdentifier());

        DataCommonsItem item = (DataCommonsItem) query.uniqueResult();
        if (item == null) {
            item = new DataCommonsItem();
            item.setExtId(content.getIdentifier());
            session.save(item);
        }

        try {
            JAXBContext context = JAXBContext.newInstance(DublinCore.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();

            DublinCore dublinCore = (DublinCore) unmarshaller.unmarshal(new StringReader(content.getContent()));
            Date lastModified = new Date();
            super.processRecord((DublinCoreItem) item, dublinCore, session, lastModified);
        } catch (JAXBException e) {
            LOGGER.error("Exception transforming document", e);
        } catch (InvocationTargetException e) {
            LOGGER.error("Error invoking method", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("Error accessing method", e);
        }

        session.merge(item);

        LOGGER.debug("Item Numbers: {}", item.getItemAttributes().size());

        session.getTransaction().commit();
    } finally {
        session.close();
    }
}