Thursday, October 28, 2010

getting started with EclipseLink

I used to work extensively on Hibernate during my early days of java/j2ee career. It has been a long time, since I have worked on any ORM based tool in java using Java Persistence API. In those days Hibernate  was seen as very light weight when compared to using EJB2.1 as it facilitated the storage and retrieval of Java domain objects via Object/Relational Mapping using very less configurations and java objects.  But now, Hibernate has grown into a collection of related projects enabling developers to utilize POJO-style domain models in their applications in ways extending well beyond Object/Relational Mapping. Recently I got a chance to work with Eclipse link and first impression was that it was a rip-off from the Hibernate. The API’s and standard steps look so familiar. The Eclipse Link JPA provides developers with a standards based Object-Relational persistence solution with additional support for many advanced features and it follows JSR specs. I will just give a brief intro on to the steps which are required to get it working.
  1. Create a database connection to the database which you want to connect.
  2. Create a Java or SOA Project based on your requirement.
  3. Add required libraries (Oracle JDBC/ other required jars)

I was using the SOA project and that’s why all SOA runtimes are present.
Mandatory jars would be Toplink , Oracle XML Parser v2 Java 1. API,  EJB3.0 and Oracle JDBC.
  1. Go to New ->EJB  and select Entities from Tables option.

It will ask for a Persistence Unit.

Create one, and name it according to the standards.
Use the database connection, and select the table you want to use and the wizard will create java POJO’s with annotations.

Rest of the steps is similar to the database adapter.
  1. Create a Entity manager factory based on the persistence unit you have defined while creating Entity from table. Create a manager class from the factory. Create a JPAEntity Manager instance for executing the queries.
    EntityManagerFactory factory =  Persistence.createEntityManagerFactory("EBS");
        EntityManager em = factory.createEntityManager();
        JpaEntityManager jpaEm = JpaHelper.getEntityManager(em);
  1. Use an ExpressionBuilder /Expression  for creating queries for each entity.     
  ExpressionBuilder expBuilder =   new ExpressionBuilder(Employee.class);
            Expression exp = expBuilder.get("empId").notEqual(0);

  1. Use an ReportQuery /ReadAllQuery for executing queries for each entity.     
          //Get the total number of distinct party records we've got
            ReportQuery ctQ = new ReportQuery(Employee.class, exp);
             ctQ.addAttribute("employeeId ",
        List l = (List)jpaEm.getActiveSession().executeQuery(ctQ);
       Long NumberRecords = (((BigDecimal)((ReportQueryResult)l.get(0)).getByIndex(0)).longValueExact());

        ReadAllQuery rq = new ReadAllQuery(Employee.class, exp);
        List result =
            (List< Employees>)jpaEm.getActiveSession().executeQuery(rq);

That’s pretty much you need to do start on eclipse link journey. You can run from java main program and call the method to see if it is fetching the correct rows based on the query that we have build.The Eclipse link API is vast and there are lot of other option which may be well suited to your requirement.So explore before you finalize on the right set of API's