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.
- Create a database connection to the database which you want to connect.
- Create a Java or SOA Project based on your requirement.
- 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.
- 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.
- 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.
CodeSnippet
EntityManagerFactory factory = Persistence.createEntityManagerFactory("EBS");
EntityManager em = factory.createEntityManager();
JpaEntityManager jpaEm = JpaHelper.getEntityManager(em);
- Use an ExpressionBuilder /Expression for creating queries for each entity.
CodeSnippet
ExpressionBuilder expBuilder = new ExpressionBuilder(Employee.class);
Expression exp = expBuilder.get("empId").notEqual(0);
- Use an ReportQuery /ReadAllQuery for executing queries for each entity.
CodeSnippet
//Get the total number of distinct party records we've got
ReportQuery ctQ = new ReportQuery(Employee.class, exp);
ctQ.addAttribute("employeeId ",
exp.getBuilder().get("employeeId").distinct().count());
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
No comments:
Post a Comment