Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

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

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

From source file:com.eurodyn.uns.dao.hibernate.BaseHibernateDao.java

License:Mozilla Public License

protected Object load(Class refClass, Serializable key, Session s) throws HibernateException {
    return s.load(refClass, key);
}

From source file:com.eurodyn.uns.dao.hibernate.HibernateChannelDao.java

License:Mozilla Public License

@Override
public Channel findChannel(Integer id) throws DAOException {
    Channel channel = null;//from   w w w .java 2 s  .c  om
    try {
        Session s = getSession();
        channel = (Channel) s.load(getReferenceClass(), id);
        Hibernate.initialize(channel.getDeliveryTypes());
        Hibernate.initialize(channel.getSubscriptions());
        Hibernate.initialize(channel.getRoles());
        Hibernate.initialize(channel.getMetadataElements());
    } catch (HibernateException e) {
        throw new DAOException(e);
    } finally {
        closeSession();
    }

    return channel;
}

From source file:com.examples.hql.HQLExamples.java

public static void main(String[] args) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    try {/*from w w  w  .  j  av a  2s  . c  o  m*/

        Query q = session.createQuery("from PurchaseOrderHeader");

        List<PurchaseOrderHeader> listPOH = q.list();

        for (PurchaseOrderHeader poh : listPOH) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        Query q1 = session.createQuery("from PurchaseOrderHeader where POHeaderID=:pohid");
        q1.setInteger("pohid", 1);

        PurchaseOrderHeader pohead = (PurchaseOrderHeader) q1.uniqueResult();

        System.out.println(pohead.getPoheaderId() + " | " + pohead.getPonumber() + " | " + pohead.getPodate()
                + " | " + pohead.getOrderValue());

        System.out.println("----------***********-------------");

        PurchaseOrderHeader po = (PurchaseOrderHeader) session.get(PurchaseOrderHeader.class, 2);

        System.out.println(po.getPoheaderId() + " | " + po.getPonumber() + " | " + po.getPodate() + " | "
                + po.getOrderValue());

        System.out.println("----------***********-------------");

        PurchaseOrderHeader po1 = (PurchaseOrderHeader) session.load(PurchaseOrderHeader.class, 3);

        System.out.println(po1.getPoheaderId() + " | " + po1.getPonumber() + " | " + po1.getPodate() + " | "
                + po1.getOrderValue());

        System.out.println("----------***********-------------");

        Query q2 = session.createQuery("from PurchaseOrderHeader where PONumber like '%PO%'");
        //q2.setParameter("PONumber", "PO102030");

        List<PurchaseOrderHeader> listPOH1 = q2.list();

        for (PurchaseOrderHeader poh : listPOH1) {

            System.out.println(poh.getPoheaderId() + " | " + poh.getPonumber() + " | " + poh.getPodate() + " | "
                    + poh.getOrderValue());
        }

        System.out.println("----------***********-------------");

        //Query q3=session.createQuery("select poh.ponumber,det.quantity from  PurchaseOrderHeader poh JOIN poh.purchaseOrderDetail det ");

        Query q3 = session.createQuery(
                "select poh.ponumber,det.quantity from  PurchaseOrderHeader poh LEFT JOIN poh.purchaseOrderDetail det ");

        List<Object[]> objList = q3.list();

        for (Object[] objList1 : objList) {

            System.out.println(objList1[0] + " | " + objList1[1]);
        }

        System.out.println("----------***********-------------");

        session.close();

        sf.close();

        System.out.println("Git by swamy....");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.floreantpos.model.dao._BaseRootDAO.java

License:Open Source License

/**
 * Used by the base DAO classes but here for your modification
 * Load object matching the given key and return it.
 *//*from w  w w.j  ava 2  s  .  c  o  m*/
protected Object load(Class refClass, Serializable key, Session s) {
    return s.load(refClass, key);
}

From source file:com.gemstone.gemfire.modules.HibernateJUnitTest.java

License:Apache License

@Test
public void testSomething() throws Exception {
    java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.ALL);
    log.info("SWAP:creating session factory In hibernateTestCase");

    Session session = getSessionFactory(null).openSession();
    log.info("SWAP:session opened");
    // session.setFlushMode(FlushMode.COMMIT);
    session.beginTransaction();//from   w ww .  ja v  a  2 s .c  om
    Event theEvent = new Event();
    theEvent.setTitle("title");
    theEvent.setDate(new Date());
    //session.save(theEvent);
    session.persist(theEvent);
    Long id = theEvent.getId();
    session.getTransaction().commit();
    log.info("commit complete...doing load");
    session.beginTransaction();
    Event ev = (Event) session.load(Event.class, id);
    log.info("load complete: " + ev);
    log.trace("SWAP");
    ev.setTitle("newTitle");
    session.save(ev);
    log.info("commit");
    session.getTransaction().commit();
    log.info("save complete " + ev);

    session.beginTransaction();
    ev = (Event) session.load(Event.class, id);
    log.info("load complete: " + ev);
    ev.setTitle("newTitle2");
    session.save(ev);
    log.info("commit");
    session.getTransaction().commit();
    log.info("save complete " + ev);

    ev = (Event) session.load(Event.class, id);
    log.info("second load " + ev);
    session.flush();
    session.close();
    log.info("flush complete session:" + session);

    for (int i = 0; i < 5; i++) {
        session = getSessionFactory(null).openSession();
        log.info("doing get " + id);
        // ev = (Event) session.load(Event.class, id);
        ev = (Event) session.get(Event.class, id);
        log.info("third load " + ev);
    }
    printExistingDB();
    Iterator it = GemFireCacheImpl.getInstance().rootRegions().iterator();
    while (it.hasNext()) {
        Region r = (Region) it.next();
        System.out.println("Region:" + r);
        Iterator enIt = r.entrySet().iterator();
        while (enIt.hasNext()) {
            Region.Entry re = (Entry) enIt.next();
            System.out.println("key:" + re.getKey() + " value:" + re.getValue());
        }
    }
    Thread.sleep(3000);
    //System.in.read();
    // try direct data

}

From source file:com.gemstone.gemfire.modules.HibernateJUnitTest.java

License:Apache License

@Test
public void testRelationship() throws Exception {
    //java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.ALL);
    Properties props = new Properties();
    props.put(DistributionConfig.GEMFIRE_PREFIX + "topology", "client-server");
    Session session = getSessionFactory(null).openSession();
    session.beginTransaction();/*from   w w  w. j a  va2 s.co  m*/

    Person thePerson = new Person();
    thePerson.setFirstname("foo");
    thePerson.setLastname("bar");
    thePerson.setAge(1);
    thePerson.setId(99L);
    session.save(thePerson);
    personId = thePerson.getId();
    log.info("person saved");

    Event theEvent = new Event();
    theEvent.setTitle("title");
    theEvent.setDate(new Date());
    session.save(theEvent);
    Long eventId = theEvent.getId();
    log.info("event saved");

    Event theEvent2 = new Event();
    theEvent2.setTitle("title2");
    theEvent2.setDate(new Date());
    session.save(theEvent2);
    Long eventId2 = theEvent2.getId();
    log.info("event2 saved");
    session.getTransaction().commit();

    session.beginTransaction();
    Person aPerson = (Person) session.load(Person.class, personId);
    Event anEvent = (Event) session.load(Event.class, eventId);
    Event anEvent2 = (Event) session.load(Event.class, eventId2);
    log.info("adding event to person");
    aPerson.getE().add(anEvent);
    aPerson.getE().add(anEvent2);
    log.info("calling commit");

    session.getTransaction().commit();
    log.info("commit complete");
    session.close();
    log.info("opening new session");
    session = getSessionFactory(null).openSession();
    log.info("SWAP:loading person");
    aPerson = (Person) session.load(Person.class, personId);
    log.info("loading events");
    Iterator<Event> e = aPerson.getE().iterator();
    while (e.hasNext()) {
        e.next();
    }
    session.close();
    log.info("opening new session");
    session = getSessionFactory(null).openSession();
    log.info("SWAP:loading person");
    aPerson = (Person) session.load(Person.class, personId);
    log.info("loading events");
    e = aPerson.getE().iterator();
    while (e.hasNext()) {
        e.next();
    }

    log.info(aPerson.getE() + "");
    session.close();
    //System.in.read();
    //    log.info("opening third session");
    //    session = getSessionFactory().openSession();
    //    log.info("loading person");
    //    aPerson = (Person)session.load(Person.class, personId);
    //    log.info("loading events");
    //    log.info(aPerson.getEvents()+"");
}

From source file:com.gemstone.gemfire.modules.HibernateJUnitTest.java

License:Apache License

@Test
public void testNormalRegion() {
    Properties props = new Properties();
    props.setProperty(DistributionConfig.GEMFIRE_PREFIX + "default-region-attributes-id", "LOCAL");
    Session session = getSessionFactory(props).openSession();
    session.beginTransaction();/*from ww  w . j  a v a 2  s .  c  o m*/
    Event theEvent = new Event();
    theEvent.setTitle("title");
    theEvent.setDate(new Date());
    session.save(theEvent);
    Long id = theEvent.getId();
    session.getTransaction().commit();
    session.beginTransaction();
    Event ev = (Event) session.load(Event.class, id);
    ev.setTitle("newTitle");
    session.save(ev);
    session.getTransaction().commit();
}

From source file:com.gemstone.gemfire.modules.HibernateJUnitTest.java

License:Apache License

@Test
public void testEnum() {
    AnnotationConfiguration cfg = new AnnotationConfiguration();
    cfg.addAnnotatedClass(Owner.class);
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    cfg.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
    cfg.setProperty("hibernate.connection.url", jdbcURL);
    cfg.setProperty("hibernate.connection.username", "sa");
    cfg.setProperty("hibernate.connection.password", "");
    cfg.setProperty("hibernate.connection.pool_size", "1");
    cfg.setProperty("hibernate.connection.autocommit", "true");
    cfg.setProperty("hibernate.hbm2ddl.auto", "update");

    cfg.setProperty("hibernate.cache.region.factory_class",
            "com.gemstone.gemfire.modules.hibernate.GemFireRegionFactory");
    cfg.setProperty("hibernate.show_sql", "true");
    cfg.setProperty("hibernate.cache.use_query_cache", "true");
    cfg.setProperty(DistributionConfig.GEMFIRE_PREFIX + STATISTIC_SAMPLING_ENABLED, "true");
    cfg.setProperty(DistributionConfig.GEMFIRE_PREFIX + LOG_FILE, gemfireLog);
    cfg.setProperty(DistributionConfig.GEMFIRE_PREFIX + "writable-working-dir", tmpDir.getPath());
    cfg.setProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT, "0");
    //cfg.setProperty("gemfire.cache-topology", "client-server");

    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();/*from  w w w . ja  v a  2s .co m*/
    Owner o = new Owner();
    o.setAddress("addr");
    o.setCity("pdx");
    o.setStatus(Status.PREMIUM);
    session.save(o);
    long id = o.getId();
    log.info("testEnum:commiting tx");
    session.getTransaction().commit();
    session.close();

    session = sf.openSession();
    Owner o1 = (Owner) session.load(Owner.class, id);
    log.info("loaded:" + o);
    assertEquals(o.getAddress(), o1.getAddress());
    assertEquals(o.getCity(), o1.getCity());
    assertEquals(o.getStatus(), o1.getStatus());
    o1.setAddress("address2");
    session.save(o1);
}

From source file:com.gemstone.gemfire.modules.SecondVMTest.java

License:Apache License

@Ignore
@Test/*from ww w . jav a 2  s  .  c o m*/
public void testStartVM() throws Exception {
    java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.ALL);
    Session session = HibernateJUnitTest.getSessionFactory(null).openSession();
    log.info("SWAP:new session open");
    long id = 1;
    log.info("loading new person:" + (id));
    GemFireCache cache = GemFireCacheImpl.getInstance();
    Iterator<Region<?, ?>> rSet = cache.rootRegions().iterator();
    while (rSet.hasNext()) {
        Region<?, ?> r = rSet.next();
        log.info("SWAP:Region " + r);
        Iterator<?> keySet = r.keySet().iterator();
        while (keySet.hasNext()) {
            log.info("key:" + keySet.next());
        }
    }
    log.info("loading new person:" + (id));
    session.beginTransaction();
    Person p = (Person) session.load(Person.class, id);
    p.setFirstname("SecondVMfirstname" + id);
    log.info("loading events");
    log.info(p.getE() + "");
    session.getTransaction().commit();
    //System.in.read();
}

From source file:com.gianburga.servicios.model.TecnicoDAOImpl.java

@Override
public void remove(int id) {
    Session session = mySessionFactory.getCurrentSession();
    session.beginTransaction();//from   ww w. j av  a 2 s .c o  m
    Tecnico tecnico = (Tecnico) session.load(Tecnico.class, id);
    session.delete(tecnico);
    session.getTransaction().commit();
}