Sunday, May 27, 2012

Using OpenJPA in Development

you know by now the use of openJPA but I havent mention you the way to use this in practice. Let me explain this using a simple example. I am using Netbeans IDE, which is easy for me to use openJPA as it will create the entity classes automatically. Study the following steps.
1. Create a new netbeans web project.
2. write click on the project & from the menu select New -->other--> persistence --> Persistence Unit.
you will get following dialog box.
3. let the name as it is & in the persistence provider section you need to select openJPA. If that is not in the list you need to create a new library. Go to new library & add your openjpa-all-2.1.0 jar. (you need to have openjpa unzipped in a folder).
4. Then click ok to complete the persistence creation.
5. Now you need to create a new package to insert the entity files. (example -com.openjpa.domain)
6.Right click on the package & select New -->other--> persistence --> Entity class from database.
Here you need to select your database connection. database schema is your db connection.
for example see below figure.
7. once the data source is selected, you can see the tables within the database will be displayed to you & you can select which tables you need to import as an entity class.
8. once you import the entity classes, netbeans will create them automatically.
8. If you take a look at these classes you can see table mapping & queries are generated.
9. then you can create your DAO files & access the data objects using the named queries or normal queries.
10. then you can use them in your servlets & jsps as you required.

So those are the steps to create a openJPA objects using netbeans. Hope this post helped you to understand the basics in openJPA. see you in next post.


MVC Architecture of OpenJPA


In the development process the standard way is to use Model View Controller architecture, where the separation of each component comes into play. 
1.      Model – Model represents the data & rules that govern access & update of data. That means basically it will contain database objects to be accessed.
2.      View – View is the web pages that we can display. The HTML, JSP pages are under the View category.
3.      Controller – This is the controlling mechanism between model & view. This will communicate with Model & View & translate & transport data objects.

Figure MVC Architecture of JPA

1.      DAO – DAO means Data Access Objects & it provides an abstract interface to the databases. Basically in DAO layer we write queries to access the database. We can write DAO’s & include methods as much as we wanted. When writing DAO we can use OpenJPA NamedQueries directly.
2.      Domain – Domain means the entity classes for the tables in the database. For each table in the database, we can create a mapping entity class & define the table entities as objects. It is much more efficient way than traditional relational database mapping. Domain also called as POJO (Plain Old Java Objects).

 In order to create the connection between database & domains, need to create an xml file including such details. That xml file is named as “persistance.xml” by default. It is similar to the normal database connection establishment, but it is defined in xml format. A typical persistance.xml file would contain following details.
This holds the connection details & there are two ways to retrieve the data objects. One method is using Entity manager & other way is using Session factory. I used entity manager factory in the DAO layers.
Hope this post would help you to understand the MVC architecture of the OpenJPA. Stay in touch :)