Package org.apache.ojb.broker

Examples of org.apache.ojb.broker.PersistenceBroker


    }

    public void bind(Object object, String name) throws ObjectNameNotUniqueException
    {
        boolean useIdentity = true;
        PersistenceBroker broker = tx.getBroker();
        ClassDescriptor cld = null;
        try
        {
            cld = broker.getClassDescriptor(ProxyHelper.getRealClass(object));
        }
        catch(PersistenceBrokerException e)
        {
        }

        // if null a non-persistent capable object was specified
        if(cld == null)
        {
            useIdentity = false;
            if(!(object instanceof Serializable))
            {
                throw new ClassNotPersistenceCapableException(
                        "Can't bind named object, because it's not Serializable. Name=" + name + ", object=" + object);
            }
        }
        else
        {
            RuntimeObject rt = new RuntimeObject(object, tx);
            // if the object is already persistet, check for read
            // lock to make sure
            // that the used object is a valid version
            // else persist the specified named object
            if(!rt.isNew())
            {
                tx.lockAndRegister(rt, Transaction.READ, tx.getRegistrationList());
            }
            else
            {
                tx.makePersistent(rt);
            }
        }
        NamedEntry oldEntry = localLookup(name);
        if(oldEntry == null)
        {
            Identity oid = broker.serviceIdentity().buildIdentity(NamedEntry.class, name);
            oldEntry = (NamedEntry) broker.getObjectByIdentity(oid);
        }
        if(oldEntry != null)
        {
            throw new ObjectNameNotUniqueException("The name of the specified named object already exist, name=" + name);
        }
View Full Code Here


     * @return The object's identifier in the form of a String
     */
    public String getObjectId(Object obj)
    {
        Identity oid = null;
        PersistenceBroker broker = null;

        try
        {
            if (getCurrentDatabase() != null)
            {
                /**
                 * is there an open database we are calling getObjectId against? if yes, use it
                 */
                broker = PersistenceBrokerFactory.createPersistenceBroker(getCurrentDatabase().getPBKey());
            }
            else
            {
                log.warn("Can't find open database, try to use the default configuration");
                /**
                 * otherwise, use default.
                 */
                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            }

            oid = broker.serviceIdentity().buildIdentity(obj);
        }
        finally
        {
            if(broker != null)
            {
                broker.close();
            }
        }
        return new String(SerializationUtils.serialize(oid));
    }
View Full Code Here

            log.debug("Retrieving all products");
        }

        // 1. build a query that select all objects of Class Product, without any further criteria
        Query             query  = new QueryByCriteria(Product.class, null);
        PersistenceBroker broker = null;
        Collection        result = null;

        try
        {
            // 2. create a broker and ask it to retrieve the Product collection
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            // 3. start broker transaction
            broker.beginTransaction();
            // 4. retrieve the products
            result = broker.getCollectionByQuery(query);
            // 5. abort transaction (because we don't want to change anything)
            broker.abortTransaction();
        }
        finally
        {
            // 6. we should not hold onto the broker instance
            if (broker != null)
            {
                broker.close();
            }
        }
        if (log.isDebugEnabled())
        {
            log.debug("Found "+(result == null ? 0 : result.size())+" products");
View Full Code Here

        example.setId(id);

        // 2. build a QueryByIdentity from this sample instance:
        Query             query  = new QueryByIdentity(example);
        PersistenceBroker broker = null;

        try
        {
            // 3. get a broker and lookup the product specified by the QBE
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            // 4. start broker transaction
            broker.beginTransaction();
            // 5. retrieve object
            result = (Product)broker.getObjectByQuery(query);
            // 6. abort transaction (because we don't want to change anything)
            broker.abortTransaction();
        }
        finally
        {
            // 7. we should not hold onto the broker instance
            if (broker != null)
            {
                broker.close();
            }
        }
        if (log.isDebugEnabled())
        {
            log.debug(result == null ? "Could not find the product" : "Found product "+result.getName());
View Full Code Here

        if (log.isDebugEnabled())
        {
            log.debug("Stroring product "+product);
        }

        PersistenceBroker broker = null;

        try
        {
            // 1. get a broker
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            // 2. start broker transaction
            broker.beginTransaction();
            // 3. now ask broker to store the edited object
            broker.store(product);
            // 4. commit transaction
            broker.commitTransaction();
        }
        catch (OJBRuntimeException ex)
        {
            // rollback in case of errors
            if (broker != null)
            {
                broker.abortTransaction();
            }
            throw ex;
        }
        finally
        {
            // 5. we should not hold onto the broker instance
            if (broker != null)
            {
                broker.close();
            }
        }
    }
View Full Code Here

        example.setId(id);

        // 2. build a QueryByIdentity from this sample instance:
        Query             query  = new QueryByIdentity(example);
        PersistenceBroker broker = null;

        try
        {
            // 3. get a broker
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
            // 4. start broker transaction
            broker.beginTransaction();
            // 5. delete product
            broker.deleteByQuery(query);
            // 6. commit transaction
            broker.commitTransaction();
        }
        catch (OJBRuntimeException ex)
        {
            // rollback in case of errors
            if (broker != null)
            {
                broker.abortTransaction();
            }
            throw ex;
        }
        finally
        {
            // 7. We should not hold onto the broker instance
            if (broker != null)
            {
                broker.close();
            }
        }
    }
View Full Code Here

        /* 1. Get the PersistenceManager */
        PersistenceManager persistenceManager = pmf.getPersistenceManager();
       
        Employee e = new Employee();
        e.setId(bean.getId());
        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
        Identity oid = new Identity(e, broker);
       
        Employee b = new Employee();
        //  2. start transaction
        persistenceManager.currentTransaction().begin();
View Full Code Here

        /* 1. Get the PersistenceManager */
        PersistenceManager persistenceManager = pmf.getPersistenceManager();
       
        Employee e = new Employee();
        e.setId(bean.getId());
        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
        Identity oid = new Identity(e, broker);
       
        Employee b = new Employee();
        //  2. start transaction
        persistenceManager.currentTransaction().begin();
View Full Code Here

        /* 1. Get the PersistenceManager */
        PersistenceManager persistenceManager = pmf.getPersistenceManager();
       
        Employee e = new Employee();
        e.setId(bean.getId());
        PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
        Identity oid = new Identity(e, broker);
       
        Employee b = new Employee();
        //  2. start transaction
        persistenceManager.currentTransaction().begin();
View Full Code Here

        database.makePersistent(gat);
        tx.commit();

        // now cross ODMG with PB api
        Gatherer fetchedGatherer = null;
        PersistenceBroker pb = null;
        try
        {
            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
            pb.clearCache();
            Criteria crit = new Criteria();
            crit.addLike("name", name);
            Query q = QueryFactory.newQuery(Gatherer.class, crit);
            fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
        }
        finally
        {
            if(pb != null) pb.close();
        }

        // check queried result
        assertNotNull(fetchedGatherer);
        assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
        assertNotNull(fetchedGatherer.getCollectiblesB());
        assertEquals(1, fetchedGatherer.getCollectiblesB().size());
        CollectibleB fetched_B = (CollectibleB) fetchedGatherer.getCollectiblesB().iterator().next();
        assertNotNull(fetched_B);

        // Now work with queried result
        tx.begin();
        tx.getBroker().clearCache();
        //*************************************
        tx.lock(fetchedGatherer, Transaction.WRITE);
        CollectibleB newB = new CollectibleB(name);
        newB.setGatherer(fetchedGatherer);
        fetchedGatherer.getCollectiblesB().add(newB);
        tx.lock(newB, Transaction.WRITE);
        //*************************************
        assertEquals(2, fetchedGatherer.getCollectiblesB().size());
        tx.commit();

        // now cross again ODMG with PB api
        fetchedGatherer = null;
        pb = null;
        try
        {
            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
            pb.clearCache();
            Criteria crit = new Criteria();
            crit.addLike("name", name);
            Query q = QueryFactory.newQuery(Gatherer.class, crit);
            fetchedGatherer = (Gatherer) pb.getObjectByQuery(q);
        }
        finally
        {
            if(pb != null) pb.close();
        }

        // check queried result
        assertNotNull(fetchedGatherer);
        assertEquals(gat.getGatId(), fetchedGatherer.getGatId());
View Full Code Here

TOP

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

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.