Package org.hibernate

Examples of org.hibernate.Session


  @Override
  public Object execute(Object[] parameters) {
    if (parameters.length == 0)
      return null;
    Session session = getContext().getSession();
    if (parameters[0].getClass().isArray()) {
      for (Object entity : (Object[]) parameters[0]) {
        session.update(entity);
      }
    } else {
      session.update(parameters[0]);
    }
    return null;
  }
View Full Code Here


*/
public class HibernateDBTest extends TestCase {

  public void testReadHibernate() throws Exception {

    Session session = HibernateHelper.currentSession();
    session.beginTransaction();
    List result = session.createQuery("from Test").list();
    for (int i = 0; i < result.size(); i++) {
      Test tst = (Test) result.get(i);
      System.out.println(tst.getFirstname() + " " + tst.getName());
    }
    session.getTransaction().commit();
    System.out.println("testReadHibernate Done");
  }
View Full Code Here

    System.out.println("testReadHibernate Done");
  }

  public void testUpdateHibernate() throws Exception {

    Session session = HibernateHelper.currentSession();
    session.beginTransaction();
    List result = session.createQuery("from Test").list();
    for (int i = 0; i < result.size(); i++) {
      Test tst = (Test) result.get(i);
      tst.setZip(new Integer((int) System.currentTimeMillis()));
    }
    session.getTransaction().commit();
    System.out.println("testUpdateHibernate Done");
  }
View Full Code Here

    System.out.println("testUpdateHibernate Done");
  }

  public void testWriteHibernate() throws Exception {

    Session session = HibernateHelper.currentSession();

    Transaction tx = session.beginTransaction();

    Test princess = new Test();
    princess.setName("Princess");
    princess.setFirstname("Fiona");
    princess.setId(new Integer(11));
    princess.setZip(new Integer(00001));
    princess.setName("shrek");

    session.save(princess);
    tx.commit();
    session.getTransaction().commit();

    HibernateHelper.closeSession();

    System.out.println("testWriteHibernate Done");
  }
View Full Code Here

*/
public class DemoCategoryHibernate {

  public static void main(String[] args) throws Exception {
    SessionHandler handler = SessionHandler.getInstance();
    Session session = handler.getSession();
    ChannelBuilder builder = new ChannelBuilder(session);
   
    // --- create a new cat
    CategoryIF catA = builder.createCategory(null, "News Category");
    Long catId = null;
   
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(catA);
      tx.commit();
      catId = new Long(catA.getId());
      System.out.println("Saved category with id " + catId + " persistently");
    }
    catch (HibernateException he) {
      if (tx != null) tx.rollback();
      throw he;
    }
    finally {
      session.close();
    }
   
    // --- update
    session = handler.getSession();
    try {
      tx = session.beginTransaction();
      Category theCat = (Category) session.load(Category.class, catId);
      theCat.setTitle("Another category title");
      tx.commit();
      System.out.println("Updated category title for id: " + catId);
    } catch (HibernateException he) {
      if (tx != null) tx.rollback();
      throw he;
    }
    finally {
      session.close()
    }
   
    // --- list
    session = handler.getSession();
    try {
      tx = session.beginTransaction();
      // Query q = session.createQuery("select cat.id from Category as cat");
      // List result = q.list();
      Query q = session.createQuery("from Category as cat where cat.title = :title");
      q.setParameter("title", "Another category title", Hibernate.STRING);
      List cats = q.list();
      tx.commit();
      Iterator it = cats.iterator();
      while (it.hasNext()) {
        Category c = (Category) it.next();
        System.out.println("--> " + c.getId());
      }
    } catch (HibernateException he2) {
      if (tx != null) tx.rollback();
      throw he2;
    }
    finally {
      session.close()
    }
  }
View Full Code Here

*/
public class DemoChannelHibernate {

  public static void main(String[] args) throws Exception {
    SessionHandler handler = SessionHandler.getInstance();
    Session session = handler.getSession();
    //ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;

    // --- list
    try {
      tx = session.beginTransaction();
      // Query q = session.createQuery("select ch.id from Channel as ch");
      // List result = q.list();
      List result = session.createQuery("from Channel").list();
      // List chs = session.find("from Channel as ch where cat.title = ?",
      //                         "Another category title", Hibernate.STRING);
      tx.commit();
      Iterator it = result.iterator();
      while (it.hasNext()) {
        Channel c = (Channel) it.next();
        System.out.println("retrieved channel --> " + c.getId());
        System.out.println("  c: " + c);
        Iterator it_items = c.getItems().iterator();
        while (it_items.hasNext()) {
          Item item = (Item) it_items.next();
          System.out.println("  * " + item);
        }
      }
    } catch (HibernateException he2) {
      if (tx != null) tx.rollback();
      throw he2;
    }
    finally {
      session.close()
    }
   
    // --- create
    /*
    session = handler.getSession();
View Full Code Here

  private Channel locateChannel(String xmlURL) {
    synchronized (builder) {
      Channel aChannel = null;
      try {
        builder.beginTransaction();
        Session sess = builder.getSession();
        List channels =
          sess.createQuery("from Channel as chan where chan.locationString = :loc")
              .setParameter("loc", xmlURL, Hibernate.STRING).list();
        log.info("***locateChannel for " + xmlURL + " produced these: " + channels);
        if (channels.size() >= 1) { // for now just take the last one
          aChannel = (Channel) channels.get(channels.size() - 1);
        }
View Full Code Here

  // TODO: split this out into InformaHibernateTestCase for better reuse

  private void verifyChannel(String label, long chan_id) {
    try {
      Session session = handler.getSession();
      log.info(label + chan_id);
      Channel aChan = (Channel) session.load(Channel.class, new Long(chan_id));
      assertEquals((long) chan_id, aChan.getId());
      assertNotNull(aChan.getTitle());
      logChannel(aChan);
      session.flush();
      session.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

   * @throws HibernateException if something with session creation goes wrong.
   */
  public static Session openSession() throws HibernateException {
    if (!inited) init();

    Session s = SESSION.get();
    if (s != null) {
      LOG.log(Level.WARNING, "Openning session more than once from the same thread!",
        new Exception("Dump"));

      s.clear();

      return s;
    } else
    {
      try {
View Full Code Here

  /**
   * Closes previousely opened session.
   */
  public static void closeSession() {
    Session s = SESSION.get();

    SESSION.set(null);

    if (s != null) {
      try {
        s.close();
      } catch (HibernateException e) {
        LOG.log(Level.SEVERE, "Could not close session.", e);
        // We can do nothing here.
        // The other session will be opened next time.
      }
View Full Code Here

TOP

Related Classes of org.hibernate.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.