The problem is common when using BlazeDS and Hibernate: Lazy loading is not possible without inflating a large object graph. The most common solution to the problem is to “break” parts of the model into manageable pieces based on how the application works. So while there should be a many to many relationship between two entities, maybe it is just left off to avoid problems. This can fragment the object model. When the object model becomes fragmented, it causes extra code to be written to make up for the lack of a correct object model.
A different approach to the problem
Another approach to keep the model pure but avoid large data loads is to use a serialization profile. This means keeping the model the the way it should be, but defining profiles on service methods to determine what parts of the graph are inflated during serialization. Most of the time a service provides functionality to one part of an application, or multiple parts that are used in a similar manner. It is more common for an object model to be used differently in different places of an application. Therefore it makes sense to single out service calls to determine object inflation.
How BlazeDS comes into it
A while back I wrote a post regarding annotations for remote objects with BlazeDS. I have expanded the annotated bean proxy to include this concept of a serialization profile. I also created a small demo project to show just how easy serialization profiles could be used.
My ‘Pet’ Project
Take a look at the following image showing the model for the pet project. As you can see, the pet class has 4 one to many relationships.
Now take a basic flex application that has a datagrid with a list of pets. Clicking on a pet results in the details being shown.
This query in hibernate is something like this: getHibernateTemplate().loadAll(Pet.class); The result would be one query for the list of pets plus and then n+ queries for all of the collections as serialization happens. In this case the whole database of records would be loaded. For 3 record this is ok, but for a large number of pets we could be waiting a long time as the entire database is loaded simply because we cannot have lazy collections.
How to use a Serialization Profile
Now let’s show how a serialization profile would help. Here is the small snipet of code for my service method:
@SerializationProfile(profile = “petList”)
public List<Pet> getAllPets()
{
return petDao.getAllPets();
}
@SerializationProfile(profile = “petDetails”)
public Pet getPetById(Integer id)
{
return petDao.getPetById(id);
}
I have used the new annotation tag @SerializationProfile to define the profile for the remote methods. The second step is to set profiles on lazy loaded one to many collections in my Pet class.
@FlexField(profiles = “petDetails”)
@OneToMany(fetch = FetchType.LAZY, mappedBy = “pet”)
public List<VetVisit> getVetVisits()
{
return vetVisits;
}
I can place one to many profiles on each getter method to determine its serialization profile. I only placed one getter example here, but all 4 of my collections on Pet.class have been set the same way. The all have a profile of petDetails. This tells the proxy, hey, I only want to load these collections when the profile method is petDetails. Our method call to getAllPets() now results in one query ran and only a list of pets returned, without all the extras. Once a user clicks on a pet, the getPetById() method is called. This method has a profile of petDetails, so all the collections are inflated upon return to flex.
Summary
By using the new Serialization Profile annotation and bean proxy, we were able to keep our model pure without inflating everything. I was able to inflate the Pet object when demanded by the application and avoid it when not needed. When other parts of the Pet application are written, they can utilize the full power of the object model without fear of various collections inflating themselves. The performance effect(of a serialization profile) is very minimal and the performance hit(while minor) is only the first time an object is serialized.
Do you think this would be helpful on a project?
A hand full of bugs have been field with Adobe’s Flash Player regarding problems uploading with Firefox.
https://bugs.adobe.com/jira/browse/FP-78
https://bugs.adobe.com/jira/browse/FP-1044
https://bugs.adobe.com/jira/browse/FP-419
https://bugs.adobe.com/jira/browse/FP-201
https://bugs.adobe.com/jira/browse/FP-568
There have also been plenty of posts describing the problem.
http://sethonflex.blogspot.com/2007/10/flex-and-filereferenceupload-using.html
http://thanksmister.com/index.php/archive/firefox-flex-urlrequest-and-sessions-issue/
http://stackoverflow.com/questions/351258/how-do-i-make-flex-file-upload-work-on-firefox-and-safari
In Flash 9, there is really no good way around this problem. However, in Flash 10 / Flex 4, an enhancement to flash.net.FileReference makes it possible to read the contents of a file before it is uploaded. Meaning that the file can be uploaded in different ways then can be done in Flash 9. The following example shows how easy file uploading can be and is not tied to SSL, Firefox, IE, Chrome, etc. This method is using Java server side with BlazeDS, but could be modified to work with other setups.
First, look at the application file.
<?xml version=”1.0″ encoding=”utf-8″?>
<s:Applicationxmlns:fx=”http://ns.adobe.com/mxml/2009“ xmlns:s=”library://ns.adobe.com/flex/spark“ xmlns:mx=”library://ns.adobe.com/flex/halo” minWidth=”1024” minHeight=”768“>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!–
Place non-visual elements (e.g., services, value objects) here –>
<s:RemoteObject id=”upload” destination=”uploadService” endpoint=”http://localhost/Context/messagebroker/amf“/>
</fx:Declarations>
<fx:Script>
<![CDATA[
private var fr:FileReference = new FileReference();
private function test():void
{
fr.browse();
fr.addEventListener(Event.SELECT, selected);
fr.addEventListener(Event.COMPLETE, complete)
}
private function selected(event:Event):void
{
fr.load();
}
private function complete(event:Event):void
{
var byteArray:ByteArray = fr.data;
upload.uploadFile(byteArray);
}
]]>
</fx:Script>
<s:Button label=”Upload” click=”test()”/>
</s:Application>
Notice the fr.load(); This will actually load the file and make it’s contents available to us in the code. The complete handler than takes those contents as a byte array and calls the remote “uploadService” passing in that byte array. Here is the simple Java code for the service method “uploadFile”:
public void uploadFile(byte[] file)
{
// Do something here to save the file
}
It is that easy in Flash 10. By allowing access to the file, the upload process is quick and painless.
I wake up to the phone ringing. I check the clock: 2:30 A.M on January 5th. Groggy and dizzy I answer the phone. It is my brother. I am in Cincinnati(Eastern Time) and he lives in San Fransisco(Pacific Time). “Why are you calling me at 2:30 in the morning”, I ask him. “It’s only 11:30 P.M. here”, he says, “and I am calling to tell you that I am father to a baby boy named Johnny!”. “Congratulations!!”, I tell him. We chat a bit more and then hang up. The whole family is excited. We had all placed bets on which day little Johnny would be born. I had picked January 5th. “Yes!!!”, I say. I have won. Or have I?? Johnny is born on January 5th eastern time, but January 4th pacific time. So who wins? Johnny’s birth date of record is January 4th. But when was he born? The answer to that depends on where I am.
Flex and Timezones
Assume now the nurse is entering the date and time of birth(when the baby is born) in the Flex baby tracker application: Jan. 4, 11:30 PM Pacific. Also assume that the developer has coded the app to use a Date object in Flex, and java.util.Date in the Java remote class and that the application servers sits in Cincinnati(eastern time). When the nurse saves the record, AMF will transfer over the date object as milliseconds since 1970. When it hits the server in eastern time, the date is now stored as 2:30 AM January 5th. If the nurse wanted to record the birth date, then as a developer, I would not want to use a date. In this instance, the date should be January 4th, globally, regardless of location or timezone. A string or integer value should be used. A Flex mx:DateChooser component is dangerous. If the nurse picks a birthdate in a mx:DateChooser, the date would be created locally on the client pc as Jan 4th, 2010 at midnight(00:00:00). When transferred to a server in eastern time, this date would become Jan 4th, 2010 at 3AM. This is not what we want. Someone in Hawaii viewing this baby record would get the date Jan 3rd, 2010 at 10PM. The birth date January 4th should appear globally to anyone the same way.
Solution:
Option 1 - Have the date chooser covert the value to a string like 01-04-2010 and then transfer the value to the server and store it as a string. This would ensure it globally being January 4th.
Option 2 - Option 1 has a problem: How do I do a query for all birthdays in January? A little tricky with a string variable. Option 2 is to covert the date chooser value to an integer in the form yyyymmdd(20100104). The date is transferred and stored in the database as an integer. Doing this makes it easier to query. For example, select * from baby where birthdate > 20100101 and birthdate < 20100131. The data still keeps the integrity for greater than and less than operations.
Have a headache yet? Go take some Tylenol:)
Air 2.0 comes with command line integration(Cheer!) among other features. This means I can execute commands if I choose from directly within an air app. Here is a simple AIR app having an input field and directly feeds the command line. The results are then output in an mx:List control.
To get this running, Download Flash Builder 4. Create an air app in Flex 4 and paste the following code into windowed app:
Ever since the AdvancedDataGrid joined the SDK it has been a bit of a love hate relationship. The great features the grid brought were essential for many projects and usually centerpieces in an application’s functionality. But the shocking part was the amount of bugs and lines of code(30,000+ lines…yikes!) that still plagues it. Doug McCune wrote a great posting on the AdvancedDataGrid and the lines of code it has, not to mention the 415 line method! The functionality of the AdvancedDataGrid is complex, so before I pile on with everyone else, let’s just point out that 1) It’s complex, 2) Sometimes lot’s of hands in the cookie jar can result in a lot of crumbs, 3) Writing code that the whole community can see instantly isn’t always easy. The mistake with the advanced data grid was probably at the beginning. Based on comparisons with the DataGrid classes, it appears most of the code was copied from DataGrid to the AdvancedDataGrid and then pieced together. I am always against large code copies, because this scenario almost always is the outcome: Bugy software that is overly complex to maintain and a nightmare to consider rewriting.
Worst pain ever sound bite The Simpsons sound bites
What is the solution for the AdvancedDataGrid?
It needs to be rewritten. Matt Chotin(SDK PM) wrote a posting about the status of the AdvancedDataGrid and Data Visualization. Not exactly thrilling news as it doesn’t look like any major enhancements to DataGrid and AdvancedDataGrid will happen until Flex 5, but I understand why the spark architecture has taken priority.
Here are the 3 main things I hope get corrected in a rewritten AdvancedDataGrid:
- Comments. There is very little commenting. I open the AdvanceDataGridBaseEx class and the comment for this class is: “The AdvancedDataGridBaseEx class is a base class of the AdvancedDataGrid control. This class contains code that provides functionality similar to the DataGrid control.” That is the description for all 8,000 lines. Based on reading this I have no idea what AdvancedDataGrid is doing extending it, and why it extends AdvancedDataGridBase. The Flex SDK should take a page from the Java SDK and write lengthy descriptions for each class. Take for example java.util.HashMap at 1,000 lines of code. Paragraphs of comments just documenting what the class does, and comments on every variable and method. While most of the Flex SDK does have good documentation, class documentation in the spark architecture has been drastically improved. Good docs are needed even more for the complicated code!
- Smaller methods and better utilization of the protected keyword. 415 lines is a ton. This isn’t the first time I’ve seen this sized method. One of the biggest hardships is when there is a bug or slight tweak needed to a component. The code is not able to be overriden or is buried in a giant 415 line method which is impossible to override anyway.
- Use Helper classes. The AdvancedDataGrid will have a large amount of code(not 30,000 large, but large). Instead of having a class that extends a base class that extends another base class that extends another base class, let’s use helper classes that group common functionality together. For example, for the advanceddatagrid, create a helper class called KeyBoardUtil. One method can handle cell navigation, a second tree navigation, and so on. It isn’t always easy to do this because of all the moving parts. By cutting out key pieces of functionality, such as keyboard controls, code readability is improved, blocks of code are smaller, methods can be better tested, and code can be reused. Helper classes help define exactly what parameters are needed for the functionality and provide a clear goal as to what in return the helper functionality will modify.



