Package org.apache.ojb.otm.core

Examples of org.apache.ojb.otm.core.Transaction


    private void createTestData() throws TransactionException, LockingException
    {
        for (int i = 0; i < COUNT; i++)
        {
            Transaction tx = _kit.getTransaction(_conn);
            tx.begin();
            Contract contract = new Contract();
            contract.setPk("C" + TIME);
            contract.setContractValue1("contractvalue1");
            contract.setContractValue2(1);
            contract.setContractValue3("contractvalue3");
            contract.setContractValue4(new Timestamp(TIME));
            _conn.makePersistent(contract);
            tx.commit();
            tx = _kit.getTransaction(_conn);
            tx.begin();
            Version version = new Version();
            version.setPk("V" + TIME);
            version.setVersionValue1("versionvalue1");
            version.setVersionValue2(1);
            version.setVersionValue3(new Timestamp(TIME));
            version.setContract(contract);
            _conn.makePersistent(version);
            tx.commit();
            tx = _kit.getTransaction(_conn);
            tx.begin();
            Effectiveness eff = new Effectiveness();
            eff.setPk("E" + TIME);
            eff.setEffValue1("effvalue1");
            eff.setEffValue2(1);
            eff.setEffValue3(new Timestamp(TIME));
            eff.setVersion(version);
            _conn.makePersistent(eff);
            tx.commit();
        }
    }
View Full Code Here


        Query q;
        Iterator iter;
        /**
        * delete effectiveness first
        */
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Effectiveness.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
        /**
        * then version
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Version.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
        /**
        * the contract
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        q = QueryFactory.newQuery(Contract.class, crit);
        iter = _conn.getIteratorByQuery(q);
        while (iter.hasNext())
        {
            _conn.deletePersistent(iter.next());
        }
        tx.commit();
    }
View Full Code Here

    public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
    {
        clearTestData();
        TestClassA a = generateTestData();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(a.getB());
        _conn.makePersistent(a);
        tx.commit();
        /**
        * clear to start test
        */
        _conn.invalidateAll();
        /**
        * get A to make it and the related B in cache
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity oid = _conn.getIdentity(a);
        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a1.getB() != null);
        assertTrue(a1.getB().getValue1().equals("hi there"));
        /**
        * everything is good, update b
        */
        tx.commit();

        /**
        * now get B and update it, do NOT get it by traversing A
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity boid = _conn.getIdentity(a.getB());
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
        assertTrue(b1 != null);
        assertTrue(b1.getValue1().equals("hi there"));
        /**
        * everything is good, update b
        */
        _conn.lockForWrite(b1);
        b1.setValue1("goodbye there");
        tx.commit();
        /**
        * make sure b was updated
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        boid = _conn.getIdentity(a.getB());
        b1 = (TestClassB) _conn.getObjectByIdentity(boid);
        assertTrue(b1 != null);
        assertTrue(b1.getValue1().equals("goodbye there"));
        tx.commit();

        /**
        * now get A again and make sure the related B is updated to reflect
        * the new value.
        */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a2.getB() != null);
        assertTrue(a2.getB().getValue1().equals("goodbye there"));
        tx.commit();
        clearTestData();
    }
View Full Code Here

    public void testSwizzleNto1() throws Exception
    {
        clearTestData();
        TestClassA a = generateTestData();
        TestClassB b2 = generateAnotherB();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        _conn.makePersistent(a.getB());
        _conn.makePersistent(a);
        tx.commit();
        /**
         * change B
         */
        tx = _kit.getTransaction(_conn);
        tx.begin();
        Identity oid = _conn.getIdentity(a);
        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
        _conn.makePersistent(b2);
        a1.setB(b2);
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        a = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a.getB() != null);
        assertTrue(a.getB().getValue1().equals("value2"));
        a.setB(null);
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        a = (TestClassA) _conn.getObjectByIdentity(oid);
        assertTrue(a.getB() == null);
        tx.commit();
    }
View Full Code Here

    }

    public void testSwizzle1toN() throws Exception
    {
        clearTestData();
        Transaction tx = _kit.getTransaction(_conn);
        tx.begin();
        ProductGroup pg = new ProductGroup();
        pg.setId(77777);
        pg.setName("1");
        _conn.makePersistent(pg);
        Article article = Article.createInstance();
        article.setArticleId(77777);
        article.setStock(333);
        pg.add(article);
        article.setProductGroup(pg);
        _conn.makePersistent(article);
        Identity pgOid = _conn.getIdentity(pg);
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
        pg.getAllArticlesInGroup().clear();
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
        assertEquals("should be equal", 0, pg.getAllArticlesInGroup().size());
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
        pg.getAllArticlesInGroup().add(article);
        tx.commit();

        tx = _kit.getTransaction(_conn);
        tx.begin();
        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
        assertEquals("should be equal", 1, pg.getAllArticlesInGroup().size());
        tx.commit();
        clearTestData();
    }
View Full Code Here

    {
      return m_baoa;
    }
    else
    {
      Transaction tx = _kit.getTransaction(_conn);
      tx.begin();
      BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
      a.setPk("abc123");
      Identity oid = _conn.getIdentity(a);
      a = (BidirectionalAssociationObjectA) _conn.getObjectByIdentity(oid);
      if (a == null)
      {
        a = new BidirectionalAssociationObjectA();
        a.setPk("abc123");
        _conn.makePersistent(a);
        BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
        b.setPk("xyz987");
        _conn.makePersistent(b);
        a.setRelatedB(b);
        b.setRelatedA(a);
      }
      tx.commit();
      m_baoa = a;
      return m_baoa;
    }
  }
View Full Code Here

    {
      return m_zoo;
    }
    else
    {
      Transaction tx = _kit.getTransaction(_conn);
      tx.begin();
      Zoo zoo = new Zoo();
      zoo.setZooId(1234);
      Identity oid = _conn.getIdentity(zoo);
      zoo = (Zoo) _conn.getObjectByIdentity(oid);
      if (zoo == null)
      {
        zoo = new Zoo();
        zoo.setZooId(1234);
        _conn.makePersistent(zoo);
        Mammal mammal = new Mammal();
        mammal.setName("molly");
        mammal.setNumLegs(4);
        mammal.setAge(55);
        zoo.addAnimal(mammal);
        _conn.makePersistent(mammal);
        Reptile reptile = new Reptile();
        reptile.setColor("green");
        reptile.setName("hubert");
        reptile.setAge(51);
        zoo.addAnimal(reptile);
        _conn.makePersistent(reptile);
      }
      tx.commit();
      m_zoo = zoo;
      return m_zoo;
    }
  }
View Full Code Here

    {
      return m_tca;
    }
    else
    {
      Transaction tx = _kit.getTransaction(_conn);
      tx.begin();
      TestClassA tca = new TestClassA();
      tca.setOid("someoid");
      Identity oid = _conn.getIdentity(tca);
      tca = (TestClassA) _conn.getObjectByIdentity(oid);
      if (tca == null)
      {
        tca = new TestClassA();
        tca.setOid("someoid");
        tca.setValue1("abc");
        tca.setValue2("123");
        tca.setValue3(5);
        _conn.makePersistent(tca);
        TestClassB tcb = new TestClassB();
        tcb.setOid("boid");
        tcb.setValue1("hi there");
        _conn.makePersistent(tcb);
        tca.setB(tcb);
      }
      tx.commit();
      m_tca = tca;
      return m_tca;
    }
  }
View Full Code Here

     *
     * @throws Throwable
     */
    public void testJustAttachConnections() throws Throwable
    {
        Transaction tx = null;
        Article example;

        OTMConnection conn1 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
        OTMConnection conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
        try
        {
            tx = _kit.getTransaction(conn1);
            tx.begin();

            tx.registerConnection(conn2);

            example = (Article) conn1.getObjectByIdentity(
                    new Identity(Article.class, Article.class,
                                 new Object[]{new Integer(77779)}));
            if (example == null)
            {
                example = Article.createInstance();
                example.setArticleId(77779);
            }
            example.setProductGroupId(7);
            example.setStock(333);
            example.setArticleName("333");
            conn1.makePersistent(example);

            EnhancedOQLQuery query = conn2.newOQLQuery();
            query.create("select obj from " + Article.class.getName()
                         + " where " + "articleId = " + example.getArticleId());
            Article same = (Article) conn2.getIteratorByOQLQuery(query).next();
            Assert.assertNotNull("Didn't find object in context of transaction", same);

            tx.commit();

        }
        catch (Throwable ex)
        {
            try
            {
                if (tx != null && tx.isInProgress())
                {
                    tx.rollback();
                }
            }
            catch (Exception ex2)
            {
            }
View Full Code Here

   public void testSwizzle4() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
    {
        clearTestData();
        TestClassA a = generateTestData();
    TestClassB b = a.getB();
        Transaction tx = _kit.getTransaction(_conn);

        tx.begin();
        _conn.makePersistent(b);
        _conn.makePersistent(a);
        b.setA(a);
        tx.commit();
        /**
        * clear to start test
        */
        _conn.invalidateAll();
        tx = _kit.getTransaction(_conn);
        tx.begin();
        /**
     * load B
     */
    Identity oidb = _conn.getIdentity(b);
        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
    /**
     * load A
      */
    Identity oida = _conn.getIdentity(a);
    TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
    assertTrue(a1 != null);
    assertTrue(a1.getB().equals(b1));
    assertTrue(b1.getA().equals(a1));
        /**
     * update B
     */
        a.setValue1("a");
        _conn.makePersistent(a);

    /**
     * B, as navigated from A, should be the same as B gotten directly.
     */
    assertTrue(a1.getValue1().equals(a.getValue1()));
        tx.commit();

    /**
     * clear
     */
        clearTestData();
View Full Code Here

TOP

Related Classes of org.apache.ojb.otm.core.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.