Tuesday, September 05, 2006

Flex Data Services - CRM Sample using Hibernate

I've played around a great deal with the sample applications that shipped with Flex Data Services. In particular I was intrigued in getting the CRM example to work with Hibernate. I'm not going to get too deep into the particulars. Here's just a good overview of what needed to be done or what purpose was served, by file.

In this file there's a section of code that needs to be uncommented. This enables a servlet that runs when jrun boots up, which creates a file called hibernate.cfg.xml and puts it in the samples\web-inf\classes\samples folder.

Once you uncomment the web.xml and start up FDS once, this file should exist. In here you will see some basic configuration stuff that basically creates a jdbc connection to an hsqldb database (a java sql engine that runs within the context of the app server). Also you will not two mapping files, explained next.


These two files serve to map sql tables & columns to java classes and properties, found in samples.crm.Employee and samples.crm.Company, respectively. You can find the source code for these files (if you're new to the java world like me) in samples\web-inf\src\(...)

In this file there are 2 sections that need to be uncommented out. You can leave the other destinations in place and just uncomment the destinations crm.employee.hibernate and crm.company.hibernate. Once you've saved these changes it's a good idea to restart FDS.

First go ahead and launch this in a browser, make sure FDS is running. The default address would be http://localhost:8700/samples/dataservice/crm/companyapp.mxml and make sure everything is still working fine. At this point the application is still using the custom company and employee assembler classes, not hibernate. If everything works fine, go ahead and edit the file. You'll need to find all the comments that pertain to Hibernate and then comment out the line preceding it, while uncommenting the line the line that follows, as per the comment instructions. Save your changes.

Now go ahead and reload the companyapp.mxml page in the browser. Keep the FDS console open and if everything went well, you can watch as hibernate generates and logs query statements.


douglasmccarroll said...

Hi Victor,

In your first post you said you weren't sure that anyone would read your blog. Well, I'm reading this post avidly, and will also devour the Hibernate-MySQL-Flex post.

And am also linking to them on my site (http://www.brightworks.com/technology/adobe_flex/with_java.html)

It doesn't look like you're on the MXNA aggregator yet. I suggest that you submit your blog. I got on with less content than you have. :)

Good stuff!

Douglas McCarroll

Anonymous said...

Nice article and I found this helpful. How would you recommend handling the following example: Let's extend the example and say we had a foreign key relationship from company to industry (two separate tables). How would you populate the combo box with industry name and resolve the key when adding a new company? I know Hibernate will allow me to do this (resolve the id key), but I'm struggling how it would be displayed in Flex. Do you set up a separate query to initialize the industry combo box from the table? Then use Hibernate query to insert the record?

Once again - nice article thanks