Package org.apache.ojb.broker

Examples of org.apache.ojb.broker.Article


            _conn2 = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
            _tx2 = _kit.getTransaction(_conn2);
            _tx2.begin();

            Article obj =  Article.createInstance();
            _lock = new ObjectLock(_conn1.getIdentity(obj));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
View Full Code Here


        ProductGroup group = new ProductGroup();
        group.setGroupName(name);
        tx.lock(group, Transaction.WRITE);
        for (int i = 0; i < COUNT; i++)
        {
            Article article = createArticle(name);
            group.add(article);
        }
        tx.commit();
        /**
         * 2. query on the article to make sure it is everything we set it up to be.
View Full Code Here

        // if(ojbSkipKnownIssueProblem()) return;
        String name = "testUpdateWithProxy_" + System.currentTimeMillis();

        ProductGroup pg1 = new ProductGroup(null, name + "_1", "a group");
        ProductGroup pg2 = new ProductGroup(null, name + "_2", "a group");
        Article a1 = createArticle(name);
        a1.setProductGroup(pg1);
        TransactionExt tx = (TransactionExt) odmg.newTransaction();
        tx.begin();
        database.makePersistent(a1);
        database.makePersistent(pg1);
        database.makePersistent(pg2);
        tx.commit();

        tx.begin();
        tx.getBroker().clearCache();
        /**
         * 1. get all articles from groups and add a new article
         */
        OQLQuery query = odmg.newOQLQuery();
        query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName like $1");
        query.bind(name + "%");
        Collection results = (Collection) query.execute();
        assertEquals(2, results.size());
        Iterator it = results.iterator();
        InterfaceProductGroup temp = null;
        /**
         * to each productgroup add an article with a pre-determined name.
         */
        while (it.hasNext())
        {
            Article article = createArticle(name);
            temp = (InterfaceProductGroup) it.next();
            tx.lock(temp, Transaction.WRITE);
            temp.add(article);
            article.setProductGroup(temp);
        }
        tx.commit();

        /**
         * 2. requery and find the articles we added.
View Full Code Here

    /**
     * Create an article with 4 product groups related to it in a 1-N relationship
     */
    protected Article createArticle(String name)
    {
        Article a = Article.createInstance();
        a.setArticleName(name);
        a.setIsSelloutArticle(true);
        a.setMinimumStock(100);
        a.setOrderedUnits(17);
        a.setPrice(0.45);
        //a.setProductGroupId(1);
        a.setStock(234);
        a.setSupplierId(4);
        a.setUnit("bottle");
        return a;
    }
View Full Code Here

        Transaction tx = null;
        Criteria crit;
        Query q;
        EnhancedOQLQuery oql;
        Iterator it;
        Article example;

        //perform transaction
        try
        {
            tx = _kit.getTransaction(_conn);
            tx.begin();

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

            tx.commit();

            Identity oid = _conn.getIdentity(example);

            // get from the cache
            tx = _kit.getTransaction(_conn);
            tx.begin();
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", 7, example.getProductGroupId().intValue());
            assertEquals("should be equal", 333, example.getStock());
            assertEquals("should be equal", "333", example.getArticleName());
            tx.commit();

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", 7, example.getProductGroupId().intValue());
            assertEquals("should be equal", "333", example.getArticleName());
            example.setArticleName("334"); // test update
            tx.commit();

            // get from the database via Query
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            crit = new Criteria();
            crit.addEqualTo("articleId", new Integer(77777));
            crit.addEqualTo("articleName", "334");
            q = QueryFactory.newQuery(Article.class, crit);
            it = _conn.getIteratorByQuery(q);
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId().intValue());
                assertEquals("should be equal", "334", article.getArticleName());
                article.setArticleName("335"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database via OQLQuery Iterator
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            oql = _conn.newOQLQuery();
            oql.create("select a from " + Article.class.getName()
                + " where articleId=$1 and articleName=$2");
            oql.bind(new Integer(77777));
            oql.bind("335");
            it = _conn.getIteratorByOQLQuery(oql);
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId().intValue());
                assertEquals("should be equal", "335", article.getArticleName());
                article.setArticleName("336"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database via OQLQuery Collection
            tx = _kit.getTransaction(_conn);
            _conn.invalidate(oid);
            tx.begin();
            oql.bind(new Integer(77777));
            oql.bind("336");
            it = ((Collection) oql.execute()).iterator();
            if (it.hasNext())
            {
                InterfaceArticle article = (InterfaceArticle) it.next();
                assertEquals("should be equal", 77777, article.getArticleId().intValue());
                assertEquals("should be equal", "336", article.getArticleName());
                article.setArticleName("337"); // test update
                if (it.hasNext())
                {
                    fail("Query returned more than 1 object");
                }
            }
            else
            {
                fail("Query returned empty result set");
            }
            tx.commit();

            // get from the database
            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidate(oid);
            example = (Article) _conn.getObjectByIdentity(oid);
            assertEquals("should be equal", "337", example.getArticleName());
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
View Full Code Here

            tx.begin();
            _conn.invalidate(oid);
            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
            assertTrue("CollectionProxy isn't loaded",
                    !((CollectionProxyDefaultImpl) pg.getAllArticlesInGroup()).isLoaded());
            Article article = Article.createInstance();
            article.setArticleId(new Integer(77777));
            article.setProductGroup(pg);
            article.setStock(333);
            article.setArticleName("333");
            pg.getAllArticlesInGroup().add(article);
            _conn.makePersistent(article);
            tx.commit();

            // get from the database
View Full Code Here

            ProductGroup pg = new ProductGroup();
            pg.setId(new Integer(77777));
            pg.setName("1");
            _conn.makePersistent(pg);
            Article article = Article.createInstance();
            article.setArticleId(new Integer(77777));
            article.setStock(373);
            pg.add(article);
            article.setProductGroup(pg);
            _conn.makePersistent(article);
            tx.commit();

            Identity aOid = _conn.getIdentity(article);
            Identity pgOid = _conn.getIdentity(pg);

            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            pg.setName("2");
            _conn.makePersistent(pg);
            tx.rollback();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            article = (Article) _conn.getObjectByIdentity(aOid);
            assertEquals("should be equal", "1", article.getProductGroup().getName());
            tx.commit();

            // test checkpoint
            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            pg.setName("2");
            _conn.makePersistent(pg);
            tx.checkpoint();
            tx.rollback();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            article = (Article) _conn.getObjectByIdentity(aOid);
            assertEquals("should be equal", "1", article.getProductGroup().getName());
            tx.commit();

            try
            {
                tx = _kit.getTransaction(_conn);
                tx.begin();
                article = (Article) _conn.getObjectByIdentity(aOid);
                _conn.deletePersistent(article);
                _conn.deletePersistent(article.getProductGroup());
                tx.commit();
            }
            catch (Throwable ex)
            {
                ex.printStackTrace();
View Full Code Here

        {
            return;
        }
        Transaction tx = null;
        ProductGroup pg;
        Article article;
        Article article2;
        org.apache.ojb.broker.Person person;
        org.apache.ojb.broker.Project project;
        Identity aOid = null;
        Identity aOid2 = null;
        Identity pgOid = null;
        Identity prsOid = null;
        Identity prjOid = null;

        //perform transaction
        try
        {
            tx = _kit.getTransaction(_conn);
            tx.begin();

            pg = new ProductGroup();
            pg.setId(new Integer(77777));
            pgOid = _conn.getIdentity(pg);
            pg.setName("1");
            _conn.makePersistent(pg);
            article = Article.createInstance();
            article.setArticleId(new Integer(77777));
            aOid = _conn.getIdentity(article);
            article.setStock(333);
            pg.add(article);
            article.setProductGroup(pg);
            article2 = Article.createInstance();
            article2.setArticleId(new Integer(77778));
            aOid2 = _conn.getIdentity(article2);
            article2.setStock(334);
            pg.add(article2);
            article2.setProductGroup(pg);
            _conn.makePersistent(article);
            _conn.makePersistent(article2);
            person = new org.apache.ojb.broker.Person(77777, "first", "last");
            prsOid = _conn.getIdentity(person);
            project = new org.apache.ojb.broker.Project(77777, "title", "desc");
            prjOid = _conn.getIdentity(project);
            _conn.makePersistent(person);
            _conn.makePersistent(project);
            tx.commit();


            tx = _kit.getTransaction(_conn);
            tx.begin();
            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
            InterfaceArticle articleNew1 = (InterfaceArticle) pg.getAllArticles().get(0);
            InterfaceArticle articleNew2 = (InterfaceArticle) pg.getAllArticles().get(1);
            if (!_conn.getIdentity(articleNew2).equals(aOid2))
            {
                articleNew2 = (InterfaceArticle) pg.getAllArticles().get(0);
                articleNew1 = (InterfaceArticle) pg.getAllArticles().get(1);
                if (!_conn.getIdentity(article2).equals(aOid2))
                {
                    fail("Missing the second article");
                }
            }
            articleNew1.setStock(433);
            articleNew2.setStock(434);
            pg.setName("2");
            tx.commit();

            tx = _kit.getTransaction(_conn);
            tx.begin();
            _conn.invalidateAll();
            articleNew1 = (InterfaceArticle) _conn.getObjectByIdentity(aOid);
            articleNew2 = (InterfaceArticle) _conn.getObjectByIdentity(aOid2);
            assertEquals("should be equal", "2", article.getProductGroup().getName());
            assertEquals("should be equal", 433, article.getStock());
            assertEquals("should be equal", 434, article2.getStock());
            tx.commit();

            // Test M:N relations
            tx = _kit.getTransaction(_conn);
            tx.begin();
View Full Code Here

    public void testSwizzling() throws Throwable
    {
        Transaction tx = null;
        ProductGroup pg;
        Article article;
        Article article2;

        try
        {
            tx = _kit.getTransaction(_conn);
            tx.begin();

            pg = new ProductGroup();
            pg.setId(new Integer(77777));
            _conn.makePersistent(pg);
            article = Article.createInstance();
            article.setArticleId(new Integer(77777));
            article.setStock(333);
            pg.add(article);
            article.setProductGroup(pg);
            _conn.makePersistent(article);
            article2 = Article.createInstance();
            article2.setArticleId(article.getArticleId());
            article2.setStock(334);
            article2.setProductGroup(pg);
            _conn.makePersistent(article2);
            article = (Article) pg.getAllArticles().get(0);
            assertEquals("should be equal", 334, article.getStock());
        }
        finally
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(new Integer(77779));
            }
            example.setProductGroupId(new Integer(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();

        }
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.Article

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.