Showing posts with label openJPA. Show all posts
Showing posts with label openJPA. Show all posts

Sunday, May 27, 2012

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 :)



Sunday, July 24, 2011

OpenJPA Components

Earlier I have explained about the concept of openJPA & its usage in the programming environment. So before going into development, let me describe about the architectural components in openJPA.
if we consider a web based project using openJPA, it will have web pages, source packages, libraries & configuration files.a typical openJPA based netbeans project folder structure would look like this.

  1. In web pages we include all our views. that means in MVC architecture the View is the web pages. So .html, .jsp files goes to the web pages.
  2. In source packages you need to define the packages required. In standard openJPA based development environment, you need to have following packages.
  • DAO - Data Access Object
DAO means objects that provides an abstract interface to databases. Separate logic within 2 parts of application can be done using same DAO interface & then changes does not affect to DAO clients. Basically what it does is separating the data access logic from business logic. Also DAO provides CRUD operations (Create/ Read/ Update/ Delete).
  • Domain
 In the domain, we include the database object files. That means if we create a database having two tables, we need to import the table objects to the domain package. For example if we have a table called "employer" then in the domain package there will be a java file called employer.java containing the table objects.
  • Service
In services package we define the mapping between DAO to web pages.

  3.   In Libraries you need to include all the jar files & other libraries required for the project. For example, in OpenJPA based project it will contain MYSQL JDBC Driver, persistence ejb-persistence.jar, openjpa-all-2.1.0.jar, tomcat 6.0, etc.

  4.  In the configuration files the important file is the Persistence.xml which holds the entire openJPA structure. persistence.xml is to define the service providers & the services to be provided. basically it will contain following details.
So I just mentioned the important things you need to know before starting a openJPA project. In the next post let's try this out & develop a MVC based project.