Package org.hibernate

Examples of org.hibernate.Transaction


    super("TestChannelGroup", name);
  }

  public void testChannelGroups() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    int chId = -1;
    int chGrpId = -1;
    // our test objects
    ChannelIF channel;
    ChannelGroupIF grp1;
    // -- first create a channel with a category assigned
    try {
      tx = session.beginTransaction();
      String chanName = "Foo Test Channel";
      channel = builder.createChannel(chanName);
      channel.setDescription("Test Channel: " + chanName);
      channel.setLocation(new URL("http://nava.de/test/channelFoo"));
      session.saveOrUpdate(channel);
      grp1 = builder.createChannelGroup("group A");
      grp1.add(channel);
      session.saveOrUpdate(grp1);
      tx.commit();
      chId = (int) channel.getId();
      chGrpId = (int) grp1.getId();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
    assertTrue("No valid channel created.", chId >= 0);
    assertTrue("No valid channel group created.", chGrpId >= 0);
    // -- try to retrieve channel and the assigned category
    try {
      logger.info("Searching for channel group " + chGrpId);
      Object result = session.get(ChannelGroup.class, new Long(chGrpId));
      assertNotNull(result);
      ChannelGroupIF cg = (ChannelGroupIF) result;
      logger.info("retrieved channel group --> " + cg);
      assertEquals(1, cg.getAll().size());
      ChannelIF c = (ChannelIF) cg.getAll().iterator().next();
      assertEquals("Foo Test Channel", c.getTitle());
      assertNull(cg.getParent());
    }
    catch (HibernateException he) {
      logger.warn("Error while querying for channel");
      throw he;
    }
    // --- delete test objects
    try {
      tx = session.beginTransaction();
      session.delete(grp1);
      session.delete(channel);
      tx.commit();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
  }
View Full Code Here


  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()
    }
View Full Code Here

    super("TestChannelCategory", name);
  }

  public void testChannelCategories() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    int chId = -1;
    // our test objects
    ChannelIF channel;
    CategoryIF cat1, cat2;
    // -- first create a channel with a category assigned
    try {
      tx = session.beginTransaction();
      // create channel
      String chanName = "Foo Test Channel";
      channel = builder.createChannel(chanName);
      channel.setDescription("Test Channel: " + chanName);
      session.saveOrUpdate(channel);
      // create cat1
      cat1 = builder.createCategory(null, "Root Cat");
      session.saveOrUpdate(cat1);
      // create cat2
      cat2 = builder.createCategory(cat1, "Agent_A");
      session.saveOrUpdate(cat2);
      channel.addCategory(cat2);
      session.saveOrUpdate(channel);
      tx.commit();
      chId = (int) channel.getId();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
    assertTrue("No valid channel created.", chId >= 0);
    // -- try to retrieve channel and the assigned category
    try {
      logger.info("Searching for channel " + chId);
      Object result = session.get(Channel.class, new Long(chId));
      assertNotNull(result);
      ChannelIF c = (ChannelIF) result;
      logger.info("retrieved channel --> " + c);
      assertEquals(1, c.getCategories().size());
      CategoryIF cat = (CategoryIF) c.getCategories().iterator().next();
      assertEquals("Agent_A", cat.getTitle());
      assertNotNull(cat.getParent());
      assertEquals("Root Cat", cat.getParent().getTitle());
    }
    catch (HibernateException he) {
      logger.warn("Error while querying for channel");
      throw he;
    }
    // -- delete test objects
    try {
      tx = session.beginTransaction();
      session.delete(cat1);
      session.delete(cat2);
      session.delete(channel);
      tx.commit();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
  }
View Full Code Here

    super("TestCreateChannels", name);
  }

  public void testCreateChannelItems() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    int chId = -1;
    // -- first create a channel with two news items
    try {
      tx = session.beginTransaction();
      String chanName = "Foo Test Channel";
      ChannelIF channel = builder.createChannel(chanName, "http://www.nava.de/channelTest");
      channel.setDescription("Test Channel: " + chanName);
      session.saveOrUpdate(channel);
      ItemIF item1 = builder.createItem(channel, "Item 1 for " + chanName,
                                        "First in line", new URL("http://www.sf1.net"));
      session.saveOrUpdate(item1);
      ItemIF item2 = builder.createItem(channel, "Item 2 for " + chanName,
                                        "Second in line", new URL("http://www.sf1.net"));
      session.saveOrUpdate(item2);
      session.saveOrUpdate(channel);
      tx.commit();
      chId = (int) channel.getId();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
    assertTrue("No valid channel created.", chId >= 0);
    // -- try to retrieve channel and children
    try {
View Full Code Here

    }
  }

  public void testCreatePersistentChannelsFromFeed() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      File inpFile = new File(getDataDir(), "xmlhack-0.91.xml");
      ChannelIF channel = FeedParser.parse(builder, inpFile);
      session.save(channel);
      tx.commit();
      assertEquals(6, channel.getItems().size());
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
  }
View Full Code Here

    }
  }

  public void testCreatePersistentChannel() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    try {
      logger.info("start new hibernate transaction");
      tx = session.beginTransaction();
      ChannelIF chA = builder.createChannel("Channel A", "http://test.org/A");
      long chA_id = chA.getId();
      chA.setDescription("test channel for hibernate backend");
      logger.info("created chA: " + chA);
      ItemIF itA = builder.createItem(chA, "Simple item", "oh what a desc",
                                      new URL("http://www.sf.net/"));
      logger.info("created itA: " + itA);
      session.save(chA);
      logger.info("saved chA");
      tx.commit();
      logger.info("transaction commited");
      assertEquals(chA_id, chA.getId());
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
  }
View Full Code Here

   * @throws PersistenceManagerException in case of any problems.
   */
  public void mergeGroups(ChannelGroupIF first, ChannelGroupIF second)
    throws PersistenceManagerException {

    Transaction tx = null;
    try {
      final Session session = HibernateUtil.openSession();
      tx = session.beginTransaction();

      HibernateUtil.lock(first, session);
      HibernateUtil.lock(second, session);
      mergeGroups(first, second, session);

      tx.commit();

      second.setId(-1);
    } catch (Exception e) {
      if (tx != null) {
        try {
          tx.rollback();
        } catch (HibernateException e1) {
          // We can do nothing here.
        }
      }

View Full Code Here

    // Trick to avoid locking of session while hashcode of Channel based on location
    // is calculated. Hashcode is cached and means that will not have problems with locking.
    channel.hashCode();

    Transaction tx = null;
    try {
      final Session session = HibernateUtil.openSession();
      tx = session.beginTransaction();

      HibernateUtil.lock(channel, session);
      group.add(channel);
      HibernateUtil.updateObject(group, session);

      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        try {
          tx.rollback();
        } catch (HibernateException e1) {
          // We can do nothing here.
        }
      }
View Full Code Here

   * @throws PersistenceManagerException in case of any problems.
   */
  public void removeChannelFromGroup(ChannelIF channel, ChannelGroupIF group)
    throws PersistenceManagerException {

    Transaction tx = null;
    try {
      final Session session = HibernateUtil.openSession();
      tx = session.beginTransaction();

      HibernateUtil.lock(channel, session);
      group.remove(channel);
      HibernateUtil.updateObject(group, session);

      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        try {
          tx.rollback();
        } catch (HibernateException e1) {
          // We can do nothing here.
        }
      }

View Full Code Here

    // session will match, but they will be different instances and
    // session will throw exception on update.
    final ChannelGroupIF[] groups = getGroups();
    ItemIF[] items = null;

    Transaction tx = null;
    try {
      final Session session = HibernateUtil.openSession();
      tx = session.beginTransaction();

      HibernateUtil.lock(channel, session);
      items = deleteChannel(channel, groups, session);

      tx.commit();

      // Reset Id's of objects
      channel.setId(-1);
      for (int i = 0; i < items.length; i++) {
        items[i].setId(-1);
      }
    } catch (Exception e) {
      if (tx != null) {
        try {
          tx.rollback();
        } catch (HibernateException e1) {
          // We can do nothing here.
        }
      }
View Full Code Here

TOP

Related Classes of org.hibernate.Transaction

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.