How to: access not loaded objects on <script>

15 views
Skip to first unread message

José Coelho

unread,
Feb 17, 2011, 8:53:45 AM2/17/11
to dpHibernate
Firstly, sorry for my bad english. lol

I'm adding dpHibernate on a created project and on mxml components all
are working correctly. But on <script> AS3 code I'm having problems
with not loaded items.

My questions will use these two simple entities: (only samples, it not
will compile )
class Author extends BaseEntity { name:String;
books:ArrayCollection; }
class Book extends BaseEntity { name:String; }

Then... go to the questions...

1. on AS3 <script>. When I call a lazy property, the proxy will be
activated to load data?

eg:
var books:ArrayCollection = author.books;

2. When i'm on AS3 <script>. How can I iterate on Proxy lists?

for each( book:Book in author.books) {
Alert.show(book.name); //here the name is null. :( How can I wait
this item to be loaded??
}


3. I don't understand how these lazy collections or entities are
automatically loaded... exist a listener for all get functions of an
AS3 "entity"? where's on dpHibernate code can I found these listener?




Thanks a lot.


Umut

unread,
Feb 17, 2011, 12:56:46 PM2/17/11
to dpHibernate
Hi Jose,

When you use flex components with binding, your entities load because
components access the entities' properties and when a propety of
entity is accessed lazy load is initiated. You don't get any errors
because components handle null properties and when the item is loaded
property change listeners (along with bindings) update the components.

When you want to access not loaded entities from action script code
you have to handle all these yourself. A couple pointers:

1. var books:ArrayCollection = author.books will not initialize the
entities in the array. For an entity to be initialized you need to
access its properties (except proxyId and proxyInitialized), I use id
since it is common for all my entities through BaseEntity.

2. even when you accessed its properties it won't be available
immediately since trip to server will take time. What you can do is
yourEntity.addEventListener(LazyLoadEvent.complete, handlerFunction);
with a handlerFunction taking event:LazyLoadEvent as parameter.

I personally wrote a utility function taking a collection and handler
function as parameters, which then goes through the entities in
collection and initiates lazy load and add a listener to each. I keep
track of the entities proxyLoaded parameters and when all of them are
loaded I call the handler functionb I got as parameter. The code is in
my other computer at work but I can post it here later if you want.

Hope it helps.

Marty Pitt

unread,
Feb 17, 2011, 2:55:46 PM2/17/11
to dphib...@googlegroups.com

> I personally wrote a utility function taking a collection and handler
> function as parameters, which then goes through the entities in
> collection and initiates lazy load and add a listener to each.

There's a similar helper distributed with dpHibernate:

http://code.google.com/p/dphibernate/source/browse/branches/2.0/flexLibrary/flex-src/org/dphibernate/util/PropertyHelper.as

ie:

PropertyHelper.invokeWhenAllPopuplated( myCallback , paramA , this ,
['author.books','publisher'] )

would call:

myCallback( paramA )

when author, author.books and publisher on this are all populated.

HTH

Marty

--------------------------------------------------
From: "Umut" <uo1...@gmail.com>
Sent: Thursday, February 17, 2011 12:56 PM
To: "dpHibernate" <dphib...@googlegroups.com>
Subject: Re: How to: access not loaded objects on <script>

> --
> You received this message because you are subscribed to the Google Groups
> "dpHibernate" group.
> To post to this group, send email to dphib...@googlegroups.com.
> To unsubscribe from this group, send email to
> dphibernate...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/dphibernate?hl=en.
>
>

Nicolas Beaudrot

unread,
Feb 18, 2011, 3:58:48 AM2/18/11
to dphib...@googlegroups.com
Thank you Umut and Marty for these informations. But in my case, the proxy is not working, I mean when I load data, even if I use one propertie all properties are loaded.

This is  my service :

@Service("productService")
@RemotingDestination
public class ProductService implements IProxyLoadService, IProxyBatchLoader{

    private IProxyLoadService proxyLoadService;
    private IProxyBatchLoader proxyBatchLoader;
    private ProductDAO dao;

    public List<Product> findAll() throws Exception{
        List<Product> results = new ArrayList<Product>();
        try{
            results = dao.findAll();
        }catch(final Exception e){

        }
        return results;
    }

    @Autowired
    public void setDao(final ProductDAO dao){
        this.dao = dao;
        proxyLoadService = new ProxyLoadService(this.dao.hibernateTemplate.getSessionFactory());
        proxyBatchLoader = new ProxyBatchLoader(this.dao.hibernateTemplate.getSessionFactory());
    }

    public ProductDAO getDao(){
        return dao;
    }

    public List<ProxyLoadResult> loadProxyBatch(ProxyLoadRequest[] requests){
        return proxyBatchLoader.loadProxyBatch(requests);
    }

    public Object loadBean(Class daoClass, Serializable id){
        return proxyLoadService.loadBean(daoClass, id);
    }

    public Map<String, Object> loadProperties(Class<?> daoClass, Serializable id){
        return proxyLoadService.loadProperties(daoClass, id);
    }
}

When I call the function findAll, the list of products is fully including unused properties, and functions loadProxyBatch, loadBean, loadProperties are never called.
Thanks in advance for your help.

PS : I'm using the latest version (RC5)
--
Nicolas Beaudrot
Département GI -  UTBM
Tél: (+33)6 03 66 33 63
http://www.nicolas-beaudrot.fr


José Coelho

unread,
Feb 18, 2011, 7:48:49 AM2/18/11
to dpHibernate
Thanks Umut!

My AS script are working now... :)

Umut

unread,
Feb 18, 2011, 8:58:29 AM2/18/11
to dpHibernate
Glad it worked.

@Marty
I tried to use the PropertyHelper function but couldn't get it to
work.

My scenario is:

I have an array collection of entities returned from server (with
pageSize 3 let's say).
I want to use the collection in actionscript and want to initialize
all it's entities (let's say it has 10 entities)

I tried to do:

var ph:PropertyHelper = new PropertyHelper();
ph.invokeWhenAllPopulated(myHandlerFunction, null, this,
["myArrayOfEntites"]);

my handler function is something like:
public function myHandlerFunction():void
{
doSomething();
Reply all
Reply to author
Forward
0 new messages