DpHibernate – An Overview

I’m going to start posting a series of articles that discuss some of the work I’ve been doing recently on dpHibernate.

I’m order to prevent repeating the basics of how dpHibernate works in each blog, here’s a little primer on the subject.

Project

dpHibernate is a BlazeDS adapter to provide support for Hibernate entities in actionscript. It’s open source, and the code is available from http://dphibernate.googlecode.com.  Many of the topics discussed on this blog specifically target the “changeMessaging” branch, which at the time of writing has not been merged back into the trunk.  The branch is available for checking out here:

http://dphibernate.googlecode.com/svn/branches/changemessaging

Lazy Loading

dpHibernate’s initial goal was to provide an adapter which handles serialization of a Hibernate object effectively. Eg: Imagine the following structure:

[Managed]
public class Author extends BaseEntity {
    public var id : int;
    public var name : String;
    public var age : int;
    public var publisher : Publisher;
    public var books : ArrayCollection;
}
[Managed]
public class Book extends BaseEntity {
    public var title : String;
    public var author : Author;
}
[Managed]
public class Publisher extends BaseEntity
{
    public var authors : ArrayCollection;
    public var books : ArrayCollection;
}

If a service is written returning a Book object from hibernate to the flex client, it’s possible that a LazyLoading exception is thrown when serializing the author property. This is because the entity is lazily loaded, and has not yet been retrieved from the database.

While one possible solution here is to load the author during serialization, this also requires loading the Author.publisher property, and the Author.books() collection, which in turn will load many more books etc. This approach can quickly becomes too expensive and bandwidth-heavy to use in a real world scenario.

Facilitating Lazy Loading with the IHibernateProxy

Instead of serializing these entities, dpHibernate sends down a proxy in their place. If and when the flex client accesses the property represented by the proxy, then that entity is retrieved from the server. Again, only the primitive values of the returned entity are serialized and sent across to the flex client. Ie., when reading Book.author() for the first time, the Author object is retrieved from the server, however it’s publisher property is represented by a proxy until such time as it is required.

Similarly, the property Author.books() is sent as a collection of proxies until such time as they are accessed. Note that because a proxy exists for every entity within the collection, attributes such as the length of the collection are still reported correctly.

In order to facilitate this lazy loading, all entity classes are required to either subclass HibernateBean, or implement IHIbernateProxy, which declares two properties:

public interface IHibernateProxy
{
    function get proxyKey():Object;
    function set proxyKey( value:Object ):void;

    function get proxyInitialized():Boolean;
    function set proxyInitialized( value:Boolean ):void;
}

Normally, the developer doesn’t access or modify these properties directly – instead they are there to facilitate the lazy loading process.

Specifics of how the lazy loading works are beyond the scope of this tutorial, but will be discussed in a later blog post.

Entity Persistence

dpHibernate provides functionality for persisting entities without having to write update services for each entity, through a simple save() method of the IUpdateableHibernateProxy interface:

public interface IUpdatableHibernateProxy extends IHibernateProxy
{
    function save(responder:IResponder=null) : AsyncToken;
    function deleteRecord(responder:IResponder=null) : AsyncToken;
}

dpHibernate provides support for creating, deleting and updating entities, including support for nested entities and collections.

Eg:

public class SomePM {
    public function doSomeWork(book:Book):void
    {
        book.author.name=”newName”;
        book.save();
    }
}

The entity persistence features are discussed in detail in an upcoming blog.

Further reading

That’s the broad overview of dpHibernate.

Other, more detailed blogs cover off:

Advertisements
Tagged

8 thoughts on “DpHibernate – An Overview

  1. […] Posted in Uncategorized dpHibernate is an open source project that facilitates lazy loading of hibernate entities with BlazeDS and Flex. Note:  If you’re not familiar with the dpHibernate project, it may to have a read of this overview post […]

  2. […] Posted in Uncategorized A colleague posted an interesting question regarding lazy loading, and I realized that it’s something that we could be doing better in dpHibernate. Note:  If you’re not familiar with the dpHibernate project, it may to have a read of this overview post […]

  3. Jesus says:

    I trying to use the last version from the repository source but I dont know how to configure the spring side.

    I have the last release working (version 1.0.17) with spring blazeds integration but i would like to try the pagination feature.

    can you give me some steps to get it work???

    thanks a lot.

  4. divxer says:

    Can you give me some examples for using Java Annotations (like: @NeverSerialize, @EagerlySerialize, @AggressivelyProxy)?

    Thank you!

  5. Albert Lima says:

    Hi Marty,
    I’m developing the project’s Saab (http://www.javasimples.com.br/flex/java-flex-parte-3-spring-blazeds-integration-dphibernate), but I am not able to update the records, there is some setting to enable updates?
    Thank you,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: