Integrating with the Hibernate Framework

These days, writing SQL manually is out of style, and lots of software developers prefer using object-relational mapping (ORM) tools for data persistence. With ORM, an instance of an object is mapped to a database table. Selecting a row from a database is equivalent to creating an instance of the object in memory. On the same note, deleting the object instance will cause deletion of the corresponding row in a database table.

In the Java community, Hibernate is the most popular open source ORM tool. Hibernate supports lazy loading, caching, and object versioning. It can either create the entire database from scratch based on the provided Java objects, or just create Java objects based on the existing database.

Mapping of Java objects to the database tables and setting their relationships (one-to-many, one-to-one, many-to-one) can be done either externally in XML configuration files or by using annotations right inside the Java classes, a.k.a. entity beans. From a Flex remoting perspective, nothing changes: Flex still sends and receives DTOs from a destination specified in remoting-config.xml.

After downloading and installing the Hibernate framework under the server with BlazeDS, the integration steps are:

  1. Create a server-side entity bean Employee that uses annotations to map appropriate values to database tables and specify queries:

    @Entity
    @Table(name = "employees")
    @NamedQueries( {
    
    @NamedQuery(name = "employeess.findAll", query = "from Employee"),
    
    @NamedQuery(name = "employees.byId", query = "select c from Employee e where
    e.employeeId= :employeeId") })
    public class Employee {
    
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "employeeId", nullable = false)
     private Long employeeId;
    
     @Column(name = "firstName", nullable = true, unique = false)
     private String firstName;
  2. Create a file called persistence.xml under the META-INF directory of your BlazeDS project. In this file, define the database location and connectivity credentials.

  3. Write a Java class EmployeeService with method getEmployees() that retrieves and updates the data using Hibernate—for example:

    public List<Employee> getEmployees() {
    
    EntityManagerFactory entityManagerFactory =
             Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    
     EntityManager em = entityManagerFactory.createEntityManager();
    
     Query findAllQuery = em.createNamedQuery("employees.findAll");
    
     List<Empoyee> employeess = findAllQuery.getResultList();
    
     return employees;
    }
  4. Define a destination in the BlazeDS remoting-config.xml file that points at the class EmployeeService:

    <destination id="myEmployee">
     <properties>
      <source>com.farata.EmployeeService</source>
     </properties>
    </destination>

The rest of the process is the same as in any Flex remoting scenario.

The only issue with this approach is that it has problems supporting lazy loading. BlazeDS uses the Java adapter to serialize Java objects, along with all related objects regardless of whether you want them to be lazy-loaded.

Note

The entire process of the integration of Flex, BlazeDS, Hibernate, and MySQL Server is described in detail in an article published at the Adobe Developer’s Connection website. You can find it at http://www.adobe.com/devnet/flex/articles/flex_hibernate_print.html.

If your Flex application uses LCDS, this issue is solved by applying special Hibernate adapter for Data Management Services. Digital Primates’ dpHibernate is a custom Flex library and a custom BlazeDS Hibernate adapter that work together to give you support for lazy loading of Hibernate objects from inside your Flex applications. You can get dpHibernate at http://code.google.com/p/dphibernate/.

Note

There is one more open source product that supports Hibernate. It’s called Granite Data Services and is an alternative to BlazeDS.

Get Agile Enterprise Application Development with Flex now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.