Problem Summary
You want to use Spring and Hibernate with BlazeDS.
Solution Summary
This cookbook entry describes how you can update the store/inventory management example from Christophe Coenraets article Using BlazeDS with Spring with orM data access with Hibernate.
Explanation
Introduction
In his article Using BlazeDS with Spring Christophe Coenraets describes how you can build internet applications with a Flex front-end and a Spring back-end. The article contains a store/inventory management example with database connectivity using the Spring JDBC abstraction framework. This cookbook entry describes how you can update the store/inventory management example with orM data access with Hibernate. Hibernate is one of the most popular persistency frameworks. Since this entry extends the store/inventory management example, it assumes you successfully completed Example 2: Store/inventory management using Flex Remoting.
Step 1: Add the Hibernate libraries
First download Hibernate from http://www.hibernate.org and add the following libraries to /blazeds/tomcat/webapps/blazeds/WEB-INF/lib:
antlr.jar cglib.jar asm.jar asm-attrs.jars commons-collections.jar commons-logging.jar hibernate3.jar jta.jar dom4j.jar log4j.jar
Step 2: Create the mapping file
Since Hibernate needs to know how to load and store objects of the Product class, we need a mapping file to tell Hibernate what table and properties to use. Create a file Product.hbm.xml in /flex-spring/samples/store/java and add the following mapping:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" []> <hibernate-mapping package="flex.samples.spring.store"> <class name="Product" table="PRODUCT"> <id name="productId" type="long" column="PRODUCT_ID" unsaved-value="0"> <generator class="identity"/> </id> <property name="name" column="NAME" length="40"/> <property name="category" column="CATEGORY" length="40"/> <property name="image" column="IMAGE" length="40"/> <property name="price" column="PRICE" type="double"/> <property name="description" column="DESCRIPTION" length="255"/> <property name="qtyInStock" column="QTY_IN_STOCK" type="integer"/> </class> </hibernate-mapping>
Step 3: Create the Hibernate ProductDAO implementation
Now that Hibernate knows how to store our objects, we can implement the ProductDAO interface to use Hibernate persistency. Create a new class HibernateProductDAO in the /flex-spring/samples/store/java directory and add the following lines:
package flex.samples.spring.store; import java.util.Collection; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.SessionFactoryUtils; public class HibernateProductDAO implements ProductDAO { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return this.sessionFactory; } public void createProduct(Product product) { Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); try { session.save(product); } catch (HibernateException e) { throw SessionFactoryUtils.convertHibernateAccessException(e); } } public void deleteProduct(Product product) { Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); try { session.delete(product); } catch (HibernateException e) { throw SessionFactoryUtils.convertHibernateAccessException(e); } } public Collection findAll() { Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); try { return session.createQuery("from Product").list(); } catch (HibernateException e) { throw SessionFactoryUtils.convertHibernateAccessException(e); } } public void updateProduct(Product product) { Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); try { session.update(product); } catch (HibernateException e) { throw SessionFactoryUtils.convertHibernateAccessException(e); } } }
Step 4: Register the Spring beans
We need to register the new productDAOBean and a Hibernate sessionFactory in the Spring configuration. Transaction supported is added using the HibernateTransactionManager. Register the beans in the applicationContext.xml as follows:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value>flex/samples/spring/store/Product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> </props> </property> <property name="dataSource"><ref bean="dataSource"/></property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="productDAOBeanTarget" class="flex.samples.spring.store.HibernateProductDAO"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="productDAOBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="txManager" /> <property name="target" ref="productDAOBeanTarget" /> <property name="transactionAttributes"> <props> <prop key="create*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
Remove the original productDAOBean configuration.
Step 5: Update the build file
Navigate to /flex-spring/samples/store and open the file build.xml. Update the compile-java target to include the Hibernate library and to copy the mapping file:
<target name="compile-java"> <javac srcdir="java" destdir="${DEPLOY_DIR}/WEB-INF/classes" classpath="${DEPLOY_DIR}/WEB-INF/lib/spring.jar;${DEPLOY_DIR}/WEB-INF/lib/hibernate3.jar"/> <copy todir="${DEPLOY_DIR}/WEB-INF/classes/flex/samples/spring/store"> <fileset dir="java" includes="**/*hbm.xml"/> </copy> </target>
Step 6: Build the project
* Navigate to /flex-spring/samples/store.
* Execute the following command to compile and deploy the client-side and the server side of the application: ant
Step 7: Run the client application
* Restart Tomcat
* Open a browser, access http://localhost:8400/blazeds/storeadmin/index.html, and test the storeadmin application.
* Open a browser, access http://localhost:8400/blazeds/store/index.html, and test the store application.