Package org.exolab.castor.jdo

Examples of org.exolab.castor.jdo.Database


                "ctf.jdo.tc9x.Server",
                "ctf.jdo.tc9x.Server",
                "ctf.jdo.tc9x.Truck"
        };
       
        Database database = _category.getDatabase();
       
        database.begin();
        OQLQuery query = database.getOQLQuery("select product from "
                + Product.class.getName() + " as product order by product.id");
        QueryResults results = query.execute();
       
        if (results.hasMore()) {
            int counter = 1;
            while (results.hasMore()) {
                Product product = (Product) results.next();
                assertNotNull(product);
                assertEquals(counter, product.getId());
                assertEquals(classNames[counter - 1], product.getClass().getName());
               
                counter += 1;
            }
        } else {
            fail("Query does not return any Product instances.");
        }
        database.commit();
       
        database.close();
    }
View Full Code Here


       
        database.close();
    }
   
    public void testOQLQueryWithParameter () throws Exception {
        Database database = _category.getDatabase();
       
        database.begin();
        OQLQuery query = database.getOQLQuery("SELECT count(laptop.id) FROM "
                + Laptop.class.getName() + " laptop WHERE laptop.resolution = $1");
        query.bind("1024");
        QueryResults results = query.execute();
       
        if (results.hasMore()) {
            Object obj = results.next();
            Long count = null;
            if (obj instanceof Long) {
                count = (Long) obj;
            } else if (obj instanceof Integer) {
                count = new Long(((Integer) obj).intValue());
            } else if (obj instanceof BigDecimal) {
                count = new Long(((BigDecimal) obj).longValue());
            }
            assertNotNull(count);
            assertEquals(1, count.intValue());
        }
       
        database.commit();
       
        database.close();
    }
View Full Code Here

    }

    private void clearCache() {
        try {
            if (_suite instanceof JDOCategory) {
                Database db = ((JDOCategory) _suite).getDatabase();
                db.getCacheManager().expireCache();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

        _jdo = JDOManager.createInstance(DATABASE_NAME);
    }


    public void setup () throws Exception {
        Database db = _jdo.getDatabase();
        db.begin();
        Connection connection = db.getJdbcConnection();
        Statement statement = connection.createStatement();
        statement.executeUpdate("create table prod (id int not null, name varchar(200) not null, price numeric(18,2) not null,  group_id int not null)");
        statement.executeUpdate("create table prod_group (id int not null, name varchar(200) not null)");
        statement.executeUpdate("create table prod_detail (id int not null, prod_id int not null, name varchar(200) not null)");
        statement.executeUpdate("create table computer (id int not null, cpu varchar(200) not null)");
        statement.executeUpdate("create table category (id int not null, name varchar(200) not null)");
        statement.executeUpdate("create table category_prod (prod_id int not null, category_id int not null)");
        db.commit();
        db.close();
    }
View Full Code Here

        db.close();
    }

    public void run() throws Exception
    {
        Database      db;
        Product       product = null;
        ProductGroup  group;
        Category      category;
        ProductDetail detail;
        Computer      computer = null;
        OQLQuery      productOql;
        OQLQuery      groupOql;
        OQLQuery      categoryOql;
        OQLQuery      computerOql;
        QueryResults  results;

        db = _jdo.getDatabase();

        db.begin();
        LOG.info( "Begin transaction to remove Product objects" );

        // Look up the products and if found in the database,
        // delete them from the database
        productOql = db.getOQLQuery( "SELECT p FROM myapp.Product p WHERE p.id = $1" );

        for ( int i = 4; i < 10; ++i )
        {
            LOG.debug( "Executing OQL" );
            productOql.bind( i );
            results = productOql.execute();

            while ( results.hasMore() )
            {
                product = ( Product ) results.next();
                LOG.debug( "Deleting existing product: " + product );
                db.remove( product );
            }
        }

        LOG.info( "End transaction to remove Product objects" );
        db.commit();

        db.begin();
        LOG.info( "Begin transaction to remove Computer object" );
       
        // Look up the computer and if found in the database,
        // delete ethis object from the database
        computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c WHERE c.id = $1" );
        computerOql.bind( 44 );
        results = computerOql.execute();

        while ( results.hasMore() )
        {
            computer = ( Computer ) results.next();
            LOG.debug( "Deleting existing computer: " + computer );
            db.remove( computer );
        }

        LOG.info( "End transaction to remove Computer objects" );
        db.commit();

        db.begin();
        LOG.info( "Begin transaction to remove Category objects" );

        // Look up the categories and if found in the database,
        // delete this object from the database
        categoryOql = db.getOQLQuery( "SELECT c FROM myapp.Category c WHERE c.id = $1" );

        // Still debugging this area because deletion of Category objects is not
        // working the second time around
        for ( int i = 7; i < 10; ++i )
        {
            categoryOql.bind( i );
            results = categoryOql.execute();
            while ( results.hasMore() )
            {
                category = ( Category ) results.next();
                LOG.debug( "Deleting existing category: " + category );
                db.remove( category );
            }
        }
       
        LOG.info( "End transaction to remove Category objects" );
        db.commit();

        db.begin();
        LOG.info( "Begin transaction: one-to-one, one-to-many, and dependent relations" );
        // If no such group exists in the database, create a new
        // object and persist it
        groupOql = db.getOQLQuery( "SELECT g FROM myapp.ProductGroup g WHERE id = $1" );
        groupOql.bind( 3 );
        results = groupOql.execute();

        if ( ! results.hasMore() )
        {
            group = new ProductGroup();
            group.setId( 3 );
            group.setName( "a group" );
            db.create( group );
            LOG.debug( "Creating new group: " + group );
        }
        else
        {
            group = ( ProductGroup ) results.next();
            LOG.debug( "Query result: " + group );
        }

        // If no such product exists in the database, create a new
        // object and persist it
        // Note: product uses group, so group object has to be
        //       created first, but can be persisted later
        productOql.bind( 4 );
        results = productOql.execute();

        if ( ! results.hasMore() )
        {
            product = new Product();
            product.setId( 4 );
            product.setName( "product4" );
            product.setPrice( 200 );
            product.setGroup( group );
            detail = new ProductDetail();
            detail.setId( 1 );
            detail.setName( "keyboard" );
            product.addDetail( detail );
            detail = new ProductDetail();
            detail.setId( 2 );
            detail.setName( "mouse" );
            product.addDetail( detail );
            detail = new ProductDetail();
            detail.setId( 3 );
            detail.setName( "monitor" );
            product.addDetail( detail );
            LOG.debug( "Creating new product: " + product );
            db.create( product );
        }
        else
        {
            LOG.debug( "Query result: " + results.next() );
        }

        // If no such computer exists in the database, create a new
        // object and persist it
        // Note: computer uses group, so group object has to be
        //       created first, but can be persisted later
        computerOql.bind( 44 );
        results = computerOql.execute();

        if ( ! results.hasMore() ) {
            computer = new Computer();
            computer.setId( 44 );
            computer.setCpu( "Pentium" );
            computer.setName( "MyPC" );
            computer.setPrice( 400 );
            computer.setGroup( group );
            detail = new ProductDetail();
            detail.setId( 4 );
            detail.setName( "network card" );
            computer.addDetail( detail );
            detail = new ProductDetail();
            detail.setId( 5 );
            detail.setName( "scsi card" );
            computer.addDetail( detail );
            LOG.debug( "Creating new computer: " + computer );
            db.create( computer );
        } else {
            LOG.debug( "Query result: " + results.next() );
        }
        LOG.info( "End transaction: one-to-one, one-to-many and dependent relations" );
        db.commit();



        // Many-to-many example using one existing product
        db.begin();
        LOG.info( "Begin transaction: one-to-one and dependent relations" );

        // If no such products with ids 5-8 exist, create new
        // objects and persist them
        for ( int i = 5; i < 10; ++i )
        {
            int j = i + 1;
            productOql.bind( j );
            results = productOql.execute();

            if ( ! results.hasMore() )
            {
                product = new Product();
                product.setId( i );
                product.setName( "product" + product.getId() );
                product.setPrice( 300 );
                product.setGroup( group );
                detail = new ProductDetail();
                detail.setId( j );
                detail.setName( "detail" + detail.getId() );
                product.addDetail( detail );
                LOG.debug( "Creating new product: " + product );
                db.create( product );
            }
            else
            {
                LOG.debug( "Query result: " + results.next() );
            }
        }

        LOG.info( "End transaction: one-to-one and dependent relations " );
        db.commit();


        db.begin();
        LOG.info( "Begin transaction: many-to-many relations" );

        for ( int x = 4; x < 7; ++x )
        {
            int y = x + 3;
            product = ( Product ) db.load( Product.class, new Integer( y ) );

            // If no such categories exists in the database, create new
            // objects and persist them
            categoryOql.bind( x );
            results = categoryOql.execute();

            if ( ! results.hasMore() )
            {
                category = new Category();
                category.setId( x );
                category.setName( "category" + category.getId() );
                category.addProduct( product );
                db.create( category );
                LOG.debug( "Creating new category: " + category );
            }
            else
            {
                category = ( Category ) results.next();
                LOG.debug( "Query result: " + category );
            }
        }

        LOG.info( "End transaction: many-to-many relations" );
        db.commit();

        product.setPrice( 333 );
        LOG.info( "Updated Product price: " + product );

        db.begin();
        LOG.info( "Begin transaction: long transaction" );

        //
        // Don't forget to implement TimeStampable for the long transaction!!!
        //
        db.update( product );
        LOG.info( "End transaction: long transaction" );
        db.commit();


        db.begin();
        LOG.info( "Begin transaction: update extends relation in long transaction " );

        computerOql.bind( 44 );
        results = computerOql.execute();

        while ( results.hasMore() )
        {
            computer = new Computer();
            computer = ( Computer ) results.next();
            LOG.debug( "Found existing computer: " + computer );
        }

        LOG.info( "End transaction: update extends relation in long transaction" );
        db.commit();

        computer.setPrice( 425 );
        LOG.info( "Updated Computer price: " + product );

        db.begin();
        LOG.info( "Begin transaction: update extends relation in long transaction " );

        //
        // Don't forget to implement TimeStampable for the long transaction!!!
        //
        db.update( computer );

        LOG.info( "End transaction: update extends relation in long transaction" );
        db.commit();

        db.begin();
        LOG.info( "Begin transaction: simple load and update within the same transaction" );

        computerOql.bind( 44 );
        results = computerOql.execute();

        if ( results.hasMore() )
        {
            computer = ( Computer ) results.next();
            computer.setCpu( "Opteron" );
        }

        LOG.info( "End transaction: simple load and update within the same transaction" );
        db.commit();

        db.begin();
        LOG.info( "Begin transaction: simple load to test the previous change" );

        computerOql.bind( 44 );
        results = computerOql.execute();

        if ( results.hasMore() )
        {
            LOG.info( "Loaded computer:" + results.next() );
        }

        LOG.info( "End transaction: simple load to test the previous change" );
        db.commit();

        Marshaller     marshaller;

        marshaller = new Marshaller( new PrintWriter(System.out) );
        marshaller.setMapping( _mapping );

        db.begin();
        LOG.info( "Begin transaction: marshalling objects to XML" );

        computerOql = db.getOQLQuery( "SELECT c FROM myapp.Computer c WHERE c.id >= $1" );
        computerOql.bind( 10 );
        results = computerOql.execute();

        while( results.hasMore() )
            marshaller.marshal( results.next() );

        LOG.info( "End transaction: marshalling objects to XML" );
        db.commit();

        db.close();
        LOG.info( "Test complete" );

        // --------------------------------------------------------------------

        LOG.info( "Begin: Walking the mapping descriptor via objects" );
View Full Code Here

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testCreateLoadUpdateDelete() throws Exception {
        Database database;
       
        // create product
        database = _category.getDatabase();
        database.begin();
       
        Product pc = new Product(1, "LCD", KindEnum.MONITOR);
        database.create(pc);

        database.commit();
        database.close();

        // load created product
        database = _category.getDatabase();
        database.begin();
       
        Product pl1 = new Product(1, "LCD", KindEnum.MONITOR);
        Product pl2 = (Product) database.load(Product.class, new Integer(1));
        assertEquals(pl1, pl2);

        database.commit();
        database.close();

        // update product
        database = _category.getDatabase();
        database.begin();
       
        Product pu = (Product) database.load(Product.class, new Integer(1));
        pu.setName("Laser");
        pu.setKind(KindEnum.PRINTER);

        database.commit();
        database.close();

        // load updated product
        database = _category.getDatabase();
        database.begin();
       
        Product pl3 = new Product(1, "Laser", KindEnum.PRINTER);
        Product pl4 = (Product) database.load(Product.class, new Integer(1));
        assertEquals(pl3, pl4);

        database.commit();
        database.close();

        // delete product
        database = _category.getDatabase();
        database.begin();
       
        Product pd = (Product) database.load(Product.class, new Integer(1));
        database.remove(pd);

        database.commit();
        database.close();
    }
View Full Code Here

        database.commit();
        database.close();
    }

    public void testQuery() throws Exception {
        Database database;
       
        // create some products
        database = _category.getDatabase();
        database.begin();
       
        database.create(new Product(1, "LCD", KindEnum.MONITOR));
        database.create(new Product(2, "Laser", KindEnum.PRINTER));
        database.create(new Product(3, "Desktop", KindEnum.COMPUTER));
        database.create(new Product(4, "Notebook", KindEnum.COMPUTER));

        database.commit();
        database.close();

        // query and delete all product
        database = _category.getDatabase();
        database.begin();
       
        Product pq;
        OQLQuery query = database.getOQLQuery("select p from "
                + ctf.jdo.tc8x.Product.class.getName() + " p order by p.id");
        QueryResults results = query.execute();
       
        pq = (Product) results.next();
        assertEquals(pq, new Product(1, "LCD", KindEnum.MONITOR));
        database.remove(pq);

        pq = (Product) results.next();
        assertEquals(pq, new Product(2, "Laser", KindEnum.PRINTER));
        database.remove(pq);
       
        pq = (Product) results.next();
        assertEquals(pq, new Product(3, "Desktop", KindEnum.COMPUTER));
        database.remove(pq);
       
        pq = (Product) results.next();
        assertEquals(pq, new Product(4, "Notebook", KindEnum.COMPUTER));
        database.remove(pq);
       
        assertFalse(results.hasMore());
        results.close();
        query.close();

        database.commit();
        database.close();
    }
View Full Code Here

    /**
     * Test method.
     * @throws Exception For any exception thrown.
     */
    public void testQueryEntityOne() throws Exception {
        Database db = _category.getDatabase();
        db.begin();
       
        Parent entity = (Parent) db.load(Parent.class, new Integer(1));

        assertNotNull(entity);
        assertEquals(new Integer(1), entity.getId());
       
        db.commit();
        db.close();
    }
View Full Code Here

    /**
     * Test method.
     * @throws Exception For any exception thrown.
     */
    public void testLoadChild() throws Exception {
        Database db = _category.getDatabase();
        db.begin();
       
        Child child = (Child) db.load(Child.class, new Integer(1));

        assertNotNull(child);
        assertEquals(new Integer(1), child.getId());
       
        db.commit();
        db.close();
    }
View Full Code Here

    /**
     * Test method.
     * @throws Exception For any exception thrown.
     */
    public void testLoadEntityWithCompoundId() throws Exception {
        Database db = _category.getDatabase();
        db.begin();
       
        ParentWithCompoundId child = (ParentWithCompoundId)
            db.load(ParentWithCompoundId.class,
                    new Identity(new Integer(1), new Integer(1)));

        assertNotNull(child);
        assertEquals(new Integer(1), child.getId1());
        assertEquals(new Integer(1), child.getId2());
       
        db.commit();
        db.close();
    }
View Full Code Here

TOP

Related Classes of org.exolab.castor.jdo.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.