Package org.odmg

Examples of org.odmg.Database


    public void testPathExpressionOqlQuery() throws Exception
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        // perform transaction
        tx.begin();

        OQLQuery query = odmg.newOQLQuery();
        // use 'like' instead of '=' when perform query with wildcards
        query.create(
                "select anArticle from " + Article.class.getName() + " where productGroup.groupName like \"Fruit*\"");
        List results = (List) query.execute();

        // crosscheck
        query = odmg.newOQLQuery();
        query.create("select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
        List check = (List) query.execute();
        if (check.size() < 1)
            fail("Could not found ProductGroup's for: " +
                    "select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
        ProductGroup pg = (ProductGroup) check.get(0);

        assertEquals(pg.getAllArticlesInGroup().size(), results.size());
        assertTrue((results.size() > 0));

        tx.commit();


        // close database

        db.close();
    }
View Full Code Here


    public void testNrmAndDlists() throws Exception
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        //perform transaction
        try
        {
            tx.begin();

            OQLQuery query = odmg.newOQLQuery();
            query.create("select x from " + Article.class.getName() + " where productGroupId = 7");
            DList results = (DList) query.execute();

            int originalSize = results.size();
            assertTrue("result count have to be > 0", originalSize > 0);

//            OJB.getLogger().debug(results);

            String name = "gimme fruits_" + System.currentTimeMillis();

            db.bind(results, name);
            tx.commit();

            tx = odmg.newTransaction();
            tx.begin();

            ((TransactionImpl) tx).getBroker().clearCache();

            // look it up again
            DList newResults = (DList) db.lookup(name);

            assertEquals(originalSize, newResults.size());
            Article art = (Article) newResults.get(0);
            assertNotNull(art);
//            OJB.getLogger().info(results);

            tx.commit();

        }
        catch (Exception e)

        {
            tx.abort();
            throw e;
        }

        // close database
        try
        {
            db.close();
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

    public void testOQLQueryBind()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
        Transaction tx = odmg.newTransaction();

        //perform transaction
        try
        {
            tx.begin();

            OQLQuery query = odmg.newOQLQuery();
            query.create("select anArticle from " + Article.class.getName() + " where articleId = $678");
            query.bind(new Integer(30));

            List results = (List) query.execute();

            Article a = (Article) results.get(0);

            //crosscheck with PersistenceBroker lookup
            // 1. get OID
            Article example = new Article();
            example.setArticleId(30);
            Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker());

            // 2. lookup object by OID
            PersistenceBroker broker = ((TransactionImpl) tx).getBroker();
            broker.clearCache();
            Article b = (Article) broker.getObjectByIdentity(oid);

            assertEquals("should be same object", a, b);

            //System.out.println("now commit all changes...");
            tx.commit();
        }
        catch (Exception ex)

        {
            tx.abort();
            fail("ODMGException: " + ex.getMessage());
        }

        // close database
        try
        {
            db.close();
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

    public void YYYtestOQLQueryOnCollections() throws Exception
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();

        //perform transaction
        try
        {
            tx.begin();
            OQLQuery query = odmg.newOQLQuery();
            query.create("select aLotOfArticles from " + Article.class.getName() + " where productGroupId = 4");

            DCollection results = (DCollection) query.execute();
            results = results.query("price > 35");

            // now perform control query
            query = odmg.newOQLQuery();
            query.create(
                    "select aLotOfArticles from "
                    + Article.class.getName()
                    + " where productGroupId = 4 and price  > 35");

            DCollection check = (DCollection) query.execute();

            assertEquals(results, check);

            tx.commit();
        }
                // close database
        finally
        {
            db.close();
        }
    }
View Full Code Here

    /**try to open non-existing db*/
    public void YYYtestWrongDbName()
    {
        // get facade instance
        Implementation objectserver = OJB.getInstance();
        Database db = objectserver.newDatabase();

        //try open database with non existing repository file:
        String wrongDatabaseName = "ThereIsNoSuchFile";
        try
        {
            db.open(wrongDatabaseName, Database.OPEN_READ_WRITE);
            fail("should not be able to open database " + wrongDatabaseName);
        }
        catch (ODMGException ex)
        {
            return;
View Full Code Here

    /**try to crash odmg and broker tx*/
    public void YYYtestBrokerCrash()
    {
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        PersistenceBroker broker = null;
        ClassDescriptor cld = null;
        String tablename = null;

        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }
View Full Code Here

     * Prepare using of the ODMG-api
     */
    private void ojbPrepare()
    {
        odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        try
        {
            if (log.isDebugEnabled())
                log.debug("open new database " +
                        db + " using databaseName name " + BeanConstants.DEF_DATABASE_NAME);
            db.open(BeanConstants.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException e)
        {
            log.error("Database open failed", e);
            throw new OJBRuntimeException("Unable to open database using ODMG api", e);
View Full Code Here

        {
            databaseName = TestHelper.DEF_DATABASE_NAME;

            // get facade instance
            Implementation odmg = OJB.getInstance();
            Database db = odmg.newDatabase();
            //open database
            db.open(databaseName, Database.OPEN_READ_WRITE);

            tx1 = (TransactionImpl) odmg.newTransaction();
            tx2 = (TransactionImpl) odmg.newTransaction();
            tx1.begin();
            tx2.begin();
View Full Code Here

        {
            databaseName = TestHelper.DEF_DATABASE_NAME;

            // get facade instance
            Implementation odmg = OJB.getInstance();
            Database db = odmg.newDatabase();
            //open database
            db.open(databaseName, Database.OPEN_READ_WRITE);

            tx1 = (TransactionImpl) odmg.newTransaction();
            tx2 = (TransactionImpl) odmg.newTransaction();
            tx1.begin();
            tx2.begin();
View Full Code Here

     * unique in the DB.
     */
    public void testDuplicateInsertion() throws Exception
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        String name = "testDuplicateInsertion_" + System.currentTimeMillis();
        String nameNew = "testDuplicateInsertion_New_" + System.currentTimeMillis();

        db.open(databaseName, Database.OPEN_READ_WRITE);
        //System.out.println("TEST: Database open");

        // insert an object with UNIQUE field NAME="A site"
        //System.out.println("TEST: Insert first object");
        newSite(odmg, name, 2, 1);

        // insert another object with UNIQUE field NAME="A site"
        // This should not create a new object (UNIQUE fields conflict) but
        // should resume gracefuly
        //System.out.println("TEST: Insert second object, should fail");
        try
        {
            newSite(odmg, name, 3, 2);
            assertTrue("We should get a SqlException 'Violation of unique index'", false);
        }
        catch (Exception e)
        {
            // we wait for this exception
            assertTrue(true);
        }

        // insert an object with new UNIQUE field NAME
        // should always work
        //System.out.println("TEST: Insert third object");
        try
        {
            newSite(odmg, nameNew, 1, 2);
            assertTrue(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            assertTrue("This exception should not happend: " + e.getMessage(), false);
            throw e;
        }

        db.close();
        //System.out.println("TEST: Database closed");
    }
View Full Code Here

TOP

Related Classes of org.odmg.Database

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.