Flex 4, Java, Spring, and Hibernate in Flash Builder 4

UPDATE 1: Flash Builder 4, BlazeDS 4, and Spring 1.0.3 have all been release so you no longer have to use beta or nightly builds of these products. Use the production versions!

Last week I posted a screencast that walks through the basics of connecting a Flex 4 application to a Java / Spring back-end with the new Data Wizards in Flash Builder 4. Here is a new video that extends beyond just simple RPC method calls and adds Hibernate for persistence. Let me know what you think.


(Open full-size video in a new window)

This entry was posted in Flex, Hibernate, Java, Spring. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

67 Comments

  1. Posted January 18, 2010 at 1:42 pm | Permalink

    Thanks James for the screen-cast, cool stuff!, I like the data binding form wizard, one thing less in my wishlist, now if I could only encourage Adobe to have flash builder 4 work on linux :( , ya there are some tweaks but not quite there yet, what’s the outcome from the survey they run last month? (I participated :)
    Cheers, and thanks again for the post.

  2. Posted January 18, 2010 at 4:25 pm | Permalink

    Thanks Martin for doing the survey. Still no word on FB for Linux support. Sorry.

  3. Raihaan Cassim
    Posted January 20, 2010 at 1:38 pm | Permalink

    Hi,

    Thanks for these two screencasts. Quite informative.

    Any chance of perhaps doing something that demonstrates the use of role-based security?

    Thanks

  4. Posted January 20, 2010 at 1:46 pm | Permalink

    Hi Raihaan,

    That would be cool. I’ll add it to my list of screencasts I want to create!

    -James

  5. Posted January 27, 2010 at 5:28 pm | Permalink

    +1 for a screencast with basic security. It’s the only thing I miss to really take Flex 4 for a ride on my new project.

  6. Ignacio Cases
    Posted January 29, 2010 at 3:23 am | Permalink

    James, thank you very much for these nice screencast. I will try to mavenize it.
    +1 for security screencast.

    -Ignacio

  7. Posted January 29, 2010 at 12:19 pm | Permalink

    FYI, I created a Grails plugin based on your screencasts: http://grails.org/plugin/blazeds

  8. Posted February 6, 2010 at 7:53 am | Permalink

    @Sébastien That’s awesome! Thanks for posting that!

  9. Martin
    Posted March 6, 2010 at 8:34 am | Permalink

    Is the source available so I don’t have to type all the config files again?

    Thanks, great screencast!

  10. Posted March 7, 2010 at 7:23 pm | Permalink

    Hi Martin,

    Most of the configuration should be here:
    http://www.jamesward.com/downloads/springFlex-server.zip

  11. salim
    Posted March 20, 2010 at 12:26 pm | Permalink

    Thanks James for the cast. Just wondering if the limitations we have always had with flex-hibernate lazy loading objects have been eliminated. These examples are great and they prove the concept but it would great to have a real world example with lazy loading integrated with the flex front end. Is there any such example today?

    Thanks again.

  12. Posted March 22, 2010 at 8:24 am | Permalink

    Hi salim,

    You could extend the regular code generators to support lazy loading. By default lazy loading is not addressed. Everything needs to be eager. Hopefully some open source projects will crop up that deal with this.

    -James

  13. Posted March 22, 2010 at 8:38 am | Permalink

    You should check out something called dpHibernate on Google Code. I don’t know if it works because I personnally prefer to use DTO’s anyway. But it might be what you are looking for.

  14. Mark Spritzler
    Posted March 30, 2010 at 4:57 pm | Permalink

    I am sorry, it is a really nice screencast for a cool product. But I had to stop watching because of the bad practices that you are showing in the video on Spring and a layered architecture. Just a couple of comments

    1) context:component-scan automatically includes context:annotation-config
    2) Services should not access the database directly, so injecting or writing queries in a service class is tightly coupling your code. (Yes it is a simple example, but people will take that as the way they should go)
    3) Since you created this in 2010, HibernateTemplate is no longer beneficial. You get all the benefits just injecting the sessionFactory into your Repository (again not Services), and then called getCurrentSession to get access to the Session object.

    That is about as far as I could watch.

    Now it looks like you had to do a lot of configuration to set things up. In another video I saw from a while ago, you only needed Springs DispatcherServlet in the web.xml and you also didn’t have to declare all the services to access them from a Flex Widget. Has this changed since then, or do we have to do it the long way like in your video? I hope it is much easier than that.

    Thanks, and please excuse my “meanness” I don’t mean to be mean, and I would love to see more of your screencasts.

    Mark

  15. Mark Spritzler
    Posted March 30, 2010 at 5:03 pm | Permalink

    One thing about the lazy loading with Hibernate.

    When developing using Hibernate, fetching should be use case dependent. You map everything as lazy loading, and based on the use case, you set the query you want to run to eager fetch everything you need for the page. Nothing more, nothing less.

    If you had a fix on the client side to leave the Session open, you are probably going to be degrading performance because of the subsequent calls back to the server to load the next bits of data that has been lazy loaded. If you have eager fetched in the query, then the client side would already have all the data that it needed and not need the extra remote calls. So while, the up front query might be slightly slower, the speed you gain from less server side calls would outweight the gain you got from lazy loading.

    LazyLoadingException is the most common exception in Hibernate, especially when you don’t consider the request and what fetching strategy is being used. While, it is simple to keep it all use case driven, it is a good example of why Hibernate has a really big learning curve. It is much more than just the mapping that is necessary to understand to use Hibernate effectively.

    Mark

  16. Mark Spritzler
    Posted March 30, 2010 at 5:20 pm | Permalink

    Wow, three comments in a row.

    OK, I see why you had more configuration. Your video is just hooking Flex to Spring, but not using the latest Spring Integration with BlazeDS. In which you only need to have the Spring Dispatcher Servlet in your web.xml, etc. So I apologize for last paragraph in my first comment. I mean I apologize for it all, as I am not trying to be negative, just wanted to make sure it is using best practices so that everyone adopts this way of integrating Flex, BlazeDS, and Spring.

    Mark

  17. Mark Spritzler
    Posted March 30, 2010 at 7:32 pm | Permalink

    OK, ignore my lazy loading comment too. I see now that the problem is even if you get just the data you need for the UI, it might include associations that aren’t needed for the UI, and that Flex will traverse the entire object graph, including those that aren’t needed for the UI in that Use Case.

    Thanks

    Mark

  18. yuanqixun
    Posted March 31, 2010 at 2:24 am | Permalink

    Hello James Ward, Thanks for your article. I have successfully deploy a flex+spring+blazeds project., and it can work normally, but when I refresh the Data/Service view in flash builder. I catch a error that the service can’t find in the configure file. Yes it can’t find from the services-config.xml because of we useing annotation and I have to delete the service from the Data/service view and so can recompile project. Do you meet the problem? how to resolve it?

  19. Posted March 31, 2010 at 9:59 am | Permalink

    Hi yuanqixun,

    That seems like a bug. Can you file it: http://bugs.adobe.com/

    Thanks.

    -James

  20. Posted March 31, 2010 at 10:06 am | Permalink

    Hi Mark,

    Thanks for you feedback! This is really helpful. The lazy loading thing is a challenge to work around. Here is an article that walks through a strategy for dealing with that:
    http://ria.dzone.com/articles/flex-hibernate-integration

    As far as the HibernateTemplate, can you point me to an example that shows the right way to do it?

    The extra config in the web.xml is needed for FB to connect to BlazeDS and query it’s services. I don’t think the latest Spring BlazeDS Integration does that for you. Correct me if I’m wrong.

    Thanks again for your feedback.

    -James

  21. kemal turhan
    Posted April 5, 2010 at 1:39 am | Permalink

    It is very good stuff about flex, hibernate and spring i have ever seen. I have tried to run this stuff, but i could’nt. It would be very nice if this stuff is published as a full tutorial.

  22. Posted April 8, 2010 at 3:02 pm | Permalink

    Thanks again for another great guide. Only thing that was different for me was the generated code. I got services:Foo instead of foo:foo like yours. Hence, I had to change a few things to get it to work:

  23. Sam
    Posted April 10, 2010 at 4:39 am | Permalink

    Hello James,
    I got your guide almost working , so if I test operation it is fine, however running the application on the browser gives me the following error, any thoughts why this is happening:

    “Detected duplicate HTTP-based FlexSessions, generally due to the remote host disabling session cookies. Session cookies must be enabled to manage the client connection correctly.
    null”

    -sam

  24. Posted April 10, 2010 at 7:07 am | Permalink

    @Sam I haven’t seen that before. But it looks like others have had that issue before:
    http://www.bing.com/search?setmkt=en-US&q=%22Detected+duplicate+HTTP-based+FlexSessions%22

  25. wldandan
    Posted April 20, 2010 at 3:42 am | Permalink

    Hi,James
    So great. I have one question about channel configuration in flex side.
    In your video, the barList.mxml declared the channelSet, when I remove the channelset, the flex side will throw exception.
    Can we ignore the AMF channel in client side?

    • fabien
      Posted May 10, 2010 at 1:29 pm | Permalink

      @wldandan: You can remove config from mxml code, but keep at least service declaration as:

      <services:Foo id="fooService"
      fault="Alert.show(event.fault.faultString+'\n'+event.fault.faultDetail)"
      showBusyCursor="true"/>

      (removing all channel configuration)

      Then, you have to add these lines into services-config.xml (in springFlex-server Project):

          <services>
              <default-channels>
                  <channel ref="my-amf"/>
              </default-channels>
          </services>

      (removing <services/>)

      Fabien from Paris, France.

      • wldandan
        Posted May 10, 2010 at 10:05 pm | Permalink

        Hi, fabien
        Thanks for your sample. It is great.

  26. Posted April 20, 2010 at 7:29 am | Permalink

    @wldandan You need to provide some way / channel for the Flex application to connect to the server. The easiest is the AMF channel.

  27. nidhi nautiyal
    Posted May 17, 2010 at 12:37 pm | Permalink

    Hi

    thanks for a very informative article ..
    i want to know can i display some link as a column in datagrid which will point to some attachment (.pdf,ms excel,ms word etc ) on remote filesystem ?

    • Posted May 17, 2010 at 1:50 pm | Permalink

      For sure! You can put a link in the DataGrid that makes a request for the file download.

  28. igor
    Posted June 12, 2010 at 6:31 am | Permalink

    James, when i connect to a service the follow tag does not exist:
    Service Name

    I can only see the follow tags:
    Service Package
    Data Types Package

    Why this difference happen between us?

  29. Ricardo
    Posted June 15, 2010 at 8:53 am | Permalink

    did not leave the tag that I’m doubtful
    here it is
    s: AMFChannel uri = “http://localhost:8080/messagebroker/amf

    Excellent tutorial, thank you for contributing to community Flex

  30. Ricardo
    Posted June 15, 2010 at 7:18 pm | Permalink

    Hello, I have two queries regarding work that way … The first doubt is when I finish my project and put it on a server the code
    s: AMFChannel uri = "http://localhost:8080/messagebroker/amf
    no way I put it in a variable not to be changing all the time?

    If Monday is when I connect the data / service to and appear once more attributes in Java class, data / service will not auto update, bringing me the attribute of the java class for me? Or every time I need to create a data / service new to when this happens?

    • Posted June 17, 2010 at 5:14 am | Permalink

      You need to externalize URLs for real production apps. You can do this manually or use a Flex Framework like Swiz to help you do this.

      You can refresh the data services when they change. That should re-introspect the Java classes and update the ActionScript objects.

  31. Garry
    Posted June 25, 2010 at 10:39 pm | Permalink

    Hi James,
    I am facing few problem, when I am trying to work as per your tutorials.
    1. Tomcat – Please let me know- Do I need to download any plug-in for eclipse? I have configure the setting in Tomcat Preferences, even my added manager password also, added into my tomcat package. When I am starting it, through Eclipse, it’s showing everything in same way as I start tomcat directly, but I am not able to look at http://localhost:8080/ as I can when I run directly.

    2. I have downloaded BlazeDS (blazeds-bin-4.0.0.14931 as well as blazeds_bin_3-0-0-544) but I am not getting the same files as you are getting. I have only 12 Files. That’s why I am not able to annotate my service as well.

    Can you please help me on these issues?
    Thanks
    Garry

  32. Posted July 16, 2010 at 2:08 am | Permalink

    首先我非常感谢你这教程,在网上找了很多这方面的整合demo.还是你的最简单容易懂.
    期待你能做一部hibernate +spring +flex +spring scurity +Cairngorm .haha.我是不是太贪心了啦.

  33. Vito
    Posted July 28, 2010 at 10:17 pm | Permalink

    Thanks man! Great example! And thanks for the dependencies!

  34. Posted August 3, 2010 at 7:51 pm | Permalink

    Great article James, thanks!

    A question concerning FB4 standalone. I’ve hooked this install up for using WTP but I don’t have any server definitions. When I try to create a server, there’s nothing there. Downloading server definitions brings back a bunch of them but not Tomcat. I did download a plugin and it’s giving me a Tomcat menu option, but it doesn’t work with the server perspective as usual for Eclipse and WTP.

    Has anyone successfully used FB4 standalone, WTP, and Tomcat like you would with an Eclipse, FB4 Plugin, WTP, and Tomcat?

    • Posted August 4, 2010 at 6:51 am | Permalink

      I’d recommend that you install Eclipse, then install FB as a plugin, and then WTP. FB standalone doesn’t have everything in it that some plugins need.

      • Posted August 4, 2010 at 7:21 am | Permalink

        Thanks, yeah that’s what I’m finding.

      • George
        Posted August 4, 2010 at 2:42 pm | Permalink

        Hello James,

        I’m trying to follow your tutorial but I cannot make the example work.
        So when I try to setup the flex project in the last step when I need to configure the J2EE server I do not get to configure the root folder and the root URL but instead I get to configure the context Root, content folder and the Blaze ds war file location. Why is that? And how could I make it work?

        Thanks in advance!

        • Posted August 5, 2010 at 11:58 am | Permalink

          I’m not sure why this isn’t working for you. You might want to try and go through my Refcard to make sure you haven’t missed a step:
          http://www.jamesward.com/2010/06/14/flex-4-hibernate-3-and-spring-3-integration/

          Otherwise email me so I can work with you on figuring out what is wrong. jaward at adobe dot com

          • George
            Posted August 5, 2010 at 2:40 pm | Permalink

            Ok thanks James for the replay it was my mistake I have checked in the flex project creation wizard the Create combined Java/Flex project using WTP check box :)
            But still in the last step when I hit the Validate Configuration button this message appears “Cannot access the web server. The server may not be running, or the web root folder or root URL may be invalid.”
            First I thought I can go on because someone said on a site that it might not function correctly but when I try after the flex project is created to connect to Blaze Ds service I check the no password is required and this appears “Error executing RDS command Status Code 403:forbidden”
            By the way the Tomcat server is up and running and the springFlex-server project is deployed on it(this project contains the Blaze Ds in it right?).

            Thanks again for your help!
            And great work!

          • Posted August 5, 2010 at 2:45 pm | Permalink

            Is your RDSServlet uncommented in your web.xml file?

          • George
            Posted August 5, 2010 at 3:50 pm | Permalink

            Sorry for the last post. The RDSServlet is uncommented.

          • Posted August 5, 2010 at 3:56 pm | Permalink

            It should look like:

            <servlet>
            <servlet-name>RDSDispatchServlet</servlet-name>
            <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
            <init-param>
            <param-name>messageBrokerId</param-name>
            <param-value>_messageBroker</param-value>
            </init-param>
            <init-param>
            <param-name>useAppserverSecurity</param-name>
            <param-value>false</param-value>
            </init-param>
            <load-on-startup>10</load-on-startup>
            </servlet>

            Does it?

          • George
            Posted August 5, 2010 at 4:14 pm | Permalink

            Yes.
            RDSDispatchServlet
            RDSDispatchServlet
            flex.rds.server.servlet.FrontEndServlet

            When the Tomcat starts up the log shows this message
            INFO: Initializing Spring FrameworkServlet ‘dispatcherServlet’

          • Posted August 5, 2010 at 4:25 pm | Permalink

            Do you get an error when you navigate to:
            http://localhost:8080/app/CFIDE/main/ide.cfm

            (Replace the port and context root with your values.)

  35. Mehdi
    Posted August 15, 2010 at 5:38 pm | Permalink

    Hello Mister Ward,thanks for all your amazing tutorials they are really helpful.
    I have a problem though,when i try to create a new Flex project with BlazeDs and Glassfish v3 i get the same error than George : “Cannot access the web server. The server may not be running, or the web root folder or root URL may be invalid.”

    I followed this tutorial to create my web app : http://sujitreddyg.wordpress.com/2009/04/07/setting-up-blazeds/

    What can i do please ?
    I am desperate,i can’t find an answer.

    • Posted August 16, 2010 at 11:07 am | Permalink

      Can’t you ignore that error and continue with the new project wizard? Or are you getting the error somewhere else?

  36. Mehdi
    Posted August 16, 2010 at 3:16 pm | Permalink

    Thanks for your answer Mr Ward,that’s what i did at first but i wasn’t able to run the project.
    It’s working now thanks to your source code,i was a little panicked yesterday and i didn’t see it before posting my message.

  37. Ricardo
    Posted August 20, 2010 at 9:16 am | Permalink

    Hello, how are you?
    Here in the company we are creating several applications that shared the same model, dao, services …

    Packages are created outside of the applications containing the three Java classes and then generate the jars of these packages and places them in applications like lib …

    But on flex application is not seeing the services that are within the lib folder in a jar …
    I go in Flex 4 Date / Services, BlazeDS and when I select the service is not it …. does anyone know if there’s no way to do this? or how can it be done?

    I have looked on google but found nothing, imagine put date / Services with Jar, apaece much nothing to do with my problem … if anyone can help me??

  38. Adi
    Posted August 24, 2010 at 2:46 am | Permalink

    Hi James,

    We want to integrate maven / any other cli script as a post build event to our BlazeDS web service project in order to generate the ActionScript classes for services and value objects. Do you know of a way to do so?

    Thank you,
    Adi

  39. Ricardo
    Posted August 24, 2010 at 5:47 am | Permalink

    Hello, how are you?

    I have a class in Java that calls student but it extends a class Person …

    public class Person (

    private TipoPessoa TipoPessoa;
    private String name;
    private Gender gender;
    private TipoCPF tipoCPF;

    )

    public class Student extends Person (

    private String ra;
    private DadosContato dadosContato;
    private Address enderecoCob;
    )

    The problem is that in Flex using the Data / Services RDS does not work that way .. when command “import classes VO student looks like this:
    [ExcludeClass]
    public class extends _Super_Usuario flash.events.EventDispatcher implements com.adobe.fiber.valueobjects.IValueObject
    (
    model_internal static function
    initRemoteClassAliasSingle (cz: Class): void
    (

    Can anyone help me?

  40. Kemal
    Posted August 25, 2010 at 3:48 am | Permalink

    Hi, James
    I will be very happy if you suggest a solution about displaying a clob database field inside a flex aplication using your tutorial framework (Flex 4, Java, Spring, and Hibernate in Flash Builder 4)

    thanks.

    • Posted August 25, 2010 at 8:31 am | Permalink

      What type of data does the clob contain?

      • Kemal
        Posted August 25, 2010 at 11:27 am | Permalink

        Image data(jpeg or bmp) i meant.

        • Posted August 25, 2010 at 12:14 pm | Permalink

          Sure! Just turn the clob into a bytearray. Then in flex set the source property of an image to the bytearray.

  41. karthikeyan M
    Posted September 1, 2010 at 12:22 pm | Permalink

    Hi,
    Nice Works !
    Can i get the source of the sample projects that your are explaining , along with dependencies.

One Trackback

  1. [...] UPDATE 1: Flash Builder 4, BlazeDS 4, and Spring 1.0.3 have all been release so you no longer have to use beta or nightly builds of these products. Use the production versions! UPDATE 2: I’ve done a second part to this screencast that combines Flex 4, Flash Builder 4, Spring 3, BlazeDS 4, and Hibernate 3. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Subscribe without commenting