Annotations in dpHibernate

dpHibernate now has support for annotations to provide fine grained control over how properties / objects are serialized when sending them to the client.

There’s 3 annotations available – I’ll discuss them here:

@NeverSerialize

This does pretty much what it says on the tin.  A property or class annotated with @NeverSerialize will never be serialized by dpHibernate when sending from Java -> Flex.

This is the server side equivilant of the [Transient] metatag in Flex.

For example:

public class ApplicationUser {

    private String password;

    @NeverSerialize
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

In this example, the password property is never serialized and sent to the client. Note – this doesn’t affect the deserialization behaviour when sending from Flex -> Java — which is handy when it comes to dealing with entity persistence.  You can set the Password property on the client and send it to the server and property updates are handled fine.  However, the value is never sent back to the client.

@AggressivelyProxy

The default behaviour and intent of dpHibernate is to handle lazily loaded properties from Hibernate.  This makes sending large collections of entities across to the client extremely efficient. However, sometimes you can have a situation where at serialization time, the collection / property has been fully hydrated by Hibernate – so there’s no lazy-loading going on.  In these situations, the default behaviour by dpHibernate is to send the full object (property / collection).  However, this can be expensive, and may not be neccessary.

@AggressivelyProxy tells the serializer to proxy the target, even if there’s no lazy-loading invovled.

For example, let’s consider an imaginary facebook clone, and the piece of code that handles populating the news feed for a given user.  The returned list of NewsFeedEntries is actually populated from various different feeds, and aggregated together. Eg:

NewsFeedService.java

    public NewsFeed getNewsFeed(ApplicationUser user)
    {
        NewsFeed newsFeed = new NewsFeed();
        newsFeed.addNewsFeedEntries(friendFeed.getTimelineEntries());
        newsFeed.addNewsFeedEntries(eventsFeed.getTimelineEntries());
        newsFeed.addNewsFeedEntries(myFeed.getTimelineEntries());
        return timeline;
    }

NewsFeed.java


public class NewsFeed {

    private List<NewsFeedEntry> newsFeedEntries;

    public void setNewsFeedEntries(List<NewsFeedEntry> timelineEntries) {
        this.newsFeedEntries = timelineEntries;
    }
    @AggressivelyProxy
    public List<NewsFeedEntry> getNewsFeedEntries() {
        return newsFeedEntries;
    }
    public void addNewsFeedEntries(List<NewsFeedEntry> entries)
    {
        newsFeedEntries.addAll(entries);
    }
}

Here, the newsFeedEntries is likely to have many many entities inside it.  However – given that it’s an aggregate of entities from other services, it’s probably not a lazy loaded collection from Hibernate.

The default dpHibernate behaviour is to serialize all the entities inside newsFeedEntries.  By adding the @AggressivelyProxy annotation, dpHibernate will proxy the collection, and the entities will be loaded from the cleint when they’re needed, giving a significant performance gain.

@EagerlySerialize

Overrides the default behaviour of the serializer, to always serializing the value (ie., never send a proxy).

The default behaviour of dpHibernate is to send a proxy for as many complex properties as it can, allowing the client to request the data as/when it’s needed.  This behaviour is often more efficient (minimizng the payload, and deserialization time on the client).

However, if the client is likely to always need a certain property, this can result in unnecessary trips being made to the server – adding unwanted latency.  By usiing the @EagerlySerialize annotation, dpHibernate will always send the real value of a property, and never a proxy in it’s place.

For example, continuing the example of the facebook clone discussed above – let’s assume that whenever we send a list of NewsFeedEntry objects to the client, that the client will always want to know the user the news feed item is about.

The default behaviour of dpHibernate is to proxy this property, which would result in an immediate second call to the server.  Instead, by annotating with @EagerlySerialize, the property is not proxied, and the real value is sent to the client:


public class NewsFeedEntry {

    private ApplicationUser entryUser;

    @EagerlySerialize
    public ApplicationUser getEntryUser() {
        return entryUser;
    }

    public void setEntryUser(ApplicationUser entryUser) {
        this.entryUser = entryUser;
    }

}

Advertisements
Tagged

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: