Flex 4, Hibernate 3, and Spring 3 Integration

Jon Rose and I have created a new DZone Refcard called Flex 4 and Spring 3 Integration. In the Refcard, we walk you through the steps for building RIAs with Flash Builder 4, Flex 4, BlazeDS 4, Spring 3, the Spring BlazeDS Integration, and Hibernate. Whew! That’s a lot of pieces! The Refcard covers simple Remoting, Flex & Hibernate integration through Spring (all Java annotation driven and using the new data-centric development features in Flash Builder 4), Pub / Sub Messaging, and Flex and Spring Security. We pack a lot into six pages!

If you want to see recorded presentations / screencasts of some of these things check out:

I hope all of this is helpful. Let me know what you think.

  • very nice, just one thing, in the refcard I can read “Alternatively, you can use Maven or the SpringSource Tool Suite to automatically handle dependencies for you.” but then blazeds 4 artifacts are not available on the central maven repository where the 3.2 are, are they located somewhere else? thanks

  • James, you absolutely rock.

  • wow amazing tutorial

    this link isnt working: “http://static.springsource.org/spring-fex/refcard/fexspring-refcard_justdeps.zip”

    do you know where i can download from ?


  • Great tutorial and thanks for all the stuff you have done around flex…I’m beginner and a student, and really follow your work.
    I was trying to do as is explained in refcardz, with only difference being I’m using JBoss….the problem is that during the process I get the error – “unable to deploy to the server”, and I can’t move ahead as explaine din the RefCardz…please tell me w.r.t. the RefCardz, what I might be doing wrong ?

    • What is giving you that error?

      • Thanks a lot for replying James. I really appreciate it.
        I’ve explained the whole error with error log trace at Stackoverflow, kindly see here – http://stackoverflow.com/questions/3893058/flex-4-spring-3-with-hibernate-mysql-setup-configuration-issues

        The thing is that I’m a begginer, working alone..so have nobody experienced to help Me, and the integration with Flex 4, Spring 3, Hibernate, MySQL is proving to be so tough as the older text / help is not consistent with the current available versions. Can you please tell me any text which I can use…I do not wanna trouble people but I’m stuck on all this for the 3rd day straight today.
        Kindly guide.


        • Looks like someone got to it before me. Is it fixed now?

          • Hey James,

            Sorry to bother again…but I have gotten everything resolved and doing everything as its said in the refcardz, but I don see any results upon running the sayHello.mxml. Basically in the browser, I see a ‘HTTP Status 404’ eooro, saying – “there is nothing such as /flexspring/sayHello-debugsayHello.html”.
            I understand, that basically nothing is being deployed on the Apache Server w.r.t. flexspring.
            I know I sound dumb, but can You tell me a way out…I have searched the net far and wide, I have reinstalled my eclipse, apache – everything.

            Any solution ? Please help.
            P.S. : My apache tomcat server is working fine( checked it creating a basic java web project).

          • So, the problem posted above was solved…I got the sayHello.mxml running. Could see it. But now, I get the following error on writing the name in the text box and pressing sayHello button –

            [RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 404: url: 'http://localhost:8080/flexsping/messagebroker/amf'"]
            	at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[E:dev4.xframeworksprojectsrpcsrcmxrpcAbstractInvoker.as:345]
            	at mx.rpc::Responder/fault()[E:dev4.xframeworksprojectsrpcsrcmxrpcResponder.as:68]
            	at mx.rpc::AsyncRequest/fault()[E:dev4.xframeworksprojectsrpcsrcmxrpcAsyncRequest.as:113]
            	at mx.messaging::ChannelSet/faultPendingSends()[E:dev4.xframeworksprojectsrpcsrcmxmessagingChannelSet.as:1614]
            	at mx.messaging::ChannelSet/channelFaultHandler()[E:dev4.xframeworksprojectsrpcsrcmxmessagingChannelSet.as:1206]
            	at flash.events::EventDispatcher/dispatchEventFunction()
            	at flash.events::EventDispatcher/dispatchEvent()
            	at mx.messaging::Channel/connectFailed()[E:dev4.xframeworksprojectsrpcsrcmxmessagingChannel.as:1128]
            	at mx.messaging.channels::PollingChannel/connectFailed()[E:dev4.xframeworksprojectsrpcsrcmxmessagingchannelsPollingChannel.as:406]
            	at mx.messaging.channels::AMFChannel/statusHandler()[E:dev4.xframeworksprojectsrpcsrcmxmessagingchannelsAMFChannel.as:453]

            and Now, I have no idea where should I go from here.
            Please help James.

          • Glad you are making progress. It appears that the messagebroker servlet is not handling requests in the flexspring web app. Are the paths correct? Did you get any startup errors?

          • Hi James,
            I think there was some error with the paths only. Though I tried hard, but was not able to resolve it. But then I downloaded the source code from the link at the end of the Refcardz, and set things up. They all run fine. So, I plan to learn from properties from those already working projects.

            One more thing James, I can’t find any text, which teaches me how to do a project using – Flex 4, Blazeds, Spring, Hibernate & MySQL(In that order), I get confused with integrating Spring and Hibernate as I have no experience using them. (and everything on net, is not for current versions of flex or spring or hibernate..they are all outdated)

            What would you suggest to improve my situation. I would really appreciate some useful advice as I hit road blocks too often.
            Also, thank you so much for replying. It helps me a lot…a lot.

          • Thanks a lot for the support.
            I was able to get the thing working well —
            Flex 4 + blazeds + Spring-Flex Integration-1.5.0(Spring Framework-3.0.4) + Hibernate-3.0 + mySQL-5.1 running on Tomcat 6.

            Should write a tutorial somewhere I think :D.
            Now, I’m onto work finally, a lot is left…now need to learn how to use the max power of flex4+spring+hibernate (and to learn firstly, how to use these things together properly…).

            Thanks James. Would be looking forward to read more of your interesting Demo’s.

  • cool post, indeed helpful.
    I would also like to share a basic post on spring with hibernate integration



  • Benyamin

    Hi James,

    I am new in Hibernate and tried to use custom SQL query via hibernate and added the method to Foo class as below

    public List findBars()
    String sql=”select * from bar”;
    return template.find(sql);

    Everything works fine including the method getBars in Foo class
    But when I try to invoke findBars method, I get the following err message

    InvocationTargetException:There was an error while invoking the operation. Check your server settings and try invoking the operation again.

    Reason: Server error java.lang.NoClassDefFoundError : antlr/ANTLRException null

    Would you please let me know what wrong is?

    • It looks like you just need to add the antlr jar to your WEB-INF/lib dir. It can be a pain to get all of the dependencies for this. My refcard walks through what they all are. Let me know if that helps.

      • Benyamin

        No it didn’t work again

        Err msg

        org.hibernate.HibernateException : No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

        • You got past your antlr error? Can you switch back to the template query?

  • Benyamin

    I tried this one too

    public List getBarsById(String id)

    return this.SessionFactory.getCurrentSession().createQuery(“from beauchamp.bar where bar.id=?”).setParameter(0, id).list();


    and got

    InvocationTargetException:There was an error while invoking the operation. Check your server settings and try invoking the operation again.

    Reason: Server error org.hibernate.HibernateException : No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here null

    • Benyamin


      package blah;
      import java.util.List;
      import org.hibernate.LockMode;
      import org.hibernate.SessionFactory;
      import org.hibernate.classic.Session;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.flex.remoting.RemotingDestination;
      import org.springframework.flex.remoting.RemotingInclude;
      import org.springframework.orm.hibernate3.HibernateTemplate;
      import org.springframework.stereotype.Service;
      public class Foo {
      	protected HibernateTemplate template;
      	protected SessionFactory SessionFactory;
      	public void setSessionFactory(SessionFactory sessionFactory)
      		template = new HibernateTemplate(sessionFactory);
      	public void addBar(String name)
      		Bar bar = new Bar();
      	public List getBars()
      		return template.findByExample(new Bar());
      	public void deletBar(String id)
      		Bar bar = new Bar();
      	public void updateBar(String id,String name)
      		Bar bar=new Bar();
      	public List getBarsById(String id)
      		return this.SessionFactory.getCurrentSession().createQuery("from beauchamp.bar where bar.id=?").setParameter(0, id).list();
    • It seems like that should work. Are the other methods working? Maybe the session factory isn’t getting auto-wired in.

  • sean

    Dear James,
    I am a beginner on this and i can reproduce your tutorial shows. Thanks so much for guiding me a road on this.
    And i have a question for you. When you use the @autowired to inject the sessionfactory from set function like setSessionFactory. Your createBar and listBar doesn’t close the session. Does it mean that Spring will take care of the session management?
    And I am still confused who is doing the job to create sessionfactory first and inject it into the autowired class? Sorry for my dummy question.

    • All of that stuff is taken care of automatically by Spring. So it just works. :)

  • Brian Long

    James – Thank you!

    After days of trying to get the full stack working from Flash 4/Flex through BlazeDS to Java Spring. I’ve got it!

    Is there any way that I could convince you to help me get it working on jdk1.5.0_12 ? I’m a bit new to Flex and Spring, I’m thinking there might be some annotations or something I need to switch to XML config or maybe different jars I could use?

    • I’m really not sure what it would take to make this work with JDK 1.5. I assume it’s not trivial and you’ll have to use versions of Spring and the Spring BlazeDS Integration that are compatible with JDK 1.5. But I’m not sure what versions that would be.

  • Pingback: Download the Flex and Spring Integration Refcard()

  • Rupananda

    I am facing the problem of RDS error like ” Unable to authentication on RDS server using current security information “.
    Please help me…

    Thanks $ Regards,
    M Rupananda.

    • Do you have the correct info in the web.xml for the RDS servlet?

  • lvan

    Hi,James,i just bulid a project as your tutorial told ,but when i start the tomcat ,it shows the information as follows ,Could you give me any suggestions to fix it?Thanks!

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘_messageBrokerDefaultHandlerMapping’: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘_messageBroker’: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanInitializationException: MessageBroker initialization failed; nested exception is java.lang.RuntimeException: MessageBroker already defined from MessageBrokerServlet with init parameter messageBrokerId = ‘_messageBroker’
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4149)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4458)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

    • Looks like you either have another MessageBroker in your web.xml or in your Spring config.

  • John

    Hi James,

    I’m loving all this integration stuff. However, its very difficult to put this stuff together with intellij if you don’t have a lot of experience. Could you perhaps make a tutorial of how to configure a project from scratch using flex, jboss and hibernate within intellij? There is a ton of stuff out there for eclipse and flash builder but hardly anything for intellij users. I noticed the one flex module video you created for it but there has been very little since. Anyway, thanks for everything you’ve put out there and providing steps for flash builder 4.


  • Rupen

    Hello James,

    In this tutorial while we do addBar we are passing a param name , but if we want to pass like 10-15 params from the flex page it will be hectic to refer all 15. Is there a way where we can pass the entire form like we do in struts or spring to a bean and refer from there so that we can just work on submit rather than passing all parameters individually ?