The Java
Persistence API ( JPA ) is Java Specification for persisting the Java Objects
to relational database using popular ORM technology. JPA provides enough tools
to enable the java developers to create database driven applications quickly.
The API can be used to persist the business object to the relational database.
Retrieving the data from database in the form of business objects is so simple
with the help of JPA API.
<!-- junit for unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- jsf dependencies -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.0-b13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.0-b13</version>
<scope>compile</scope>
</dependency>
<!-- primefaces dependency for user user inetrfaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>
<!-- Log4j dependency for logging-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.5.Final</version>
</dependency>
<!-- MySQL Connector with java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
<type>jar</type>
</dependency>
<!-- Java EE web application -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
<repository>
<url>http://repo1.maven.org/maven2/</url>
<id>hibernate-persistence</id>
<layout>default</layout>
<name>Repository for library Library[hibernate-persistence]</name>
</repository>
</repositories>
In this project jetty server plugin is added for the web server. following is the jetty plugin.
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
</configuration>
</plugin>
</plugins>
Then Create a new JSF file and Named as index.xhtml. This is the primary page of this project. And remove the existing index.jsp file.
Following New Entity Class window displays. Put the name for class name Student and put the package name org.ymini.yschool.mayooran.entitymodel and then click the Next button.
Contents:
- Creating a Database Using MySQL
- Making a Connection to the Database Using NetBeans
- Creating the Maven Projects
- Creating a Java Persistence Entity Class Representing the Student Table
- Creating a Persistence Unit
- Creating the Classes
- Creating the Entity Controller Class
- Binding the Managed Bean
- Doing Some Customization
Creating a Database Using MySQL
Firstly , Create a database named studentdb. Execute the following SQL statement on MySQL Command Line Client.
create database studentdb;
Then verify using show databases; statement
Making a Connection to the Database Using NetBeans
In, NetBeans, connect to the studentdb database by following the steps shown in the following Figures.
Click the Services tab, the right click on the Databases then Choose the New Connection.
There are lot of Databases Drivers. We choose MySQL(Connector/J driver), then Click Next button.
Fill in the MySQL connection string Database name, user name, and its password and Click Next. In this case we are using root user.
Creating the Maven Projects
Create a maven web application using command line. (This task already mentioned in previous Post in this blog)
The open the created project in NetBeans and then add necessary dependencies for this project. following dependencies are added to pom.xml for my project.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- jsf dependencies -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.0-b13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.0-b13</version>
<scope>compile</scope>
</dependency>
<!-- primefaces dependency for user user inetrfaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>
<!-- Log4j dependency for logging-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.5.Final</version>
</dependency>
<!-- MySQL Connector with java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
<type>jar</type>
</dependency>
<!-- Java EE web application -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<type>jar</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
<repository>
<url>http://repo1.maven.org/maven2/</url>
<id>hibernate-persistence</id>
<layout>default</layout>
<name>Repository for library Library[hibernate-persistence]</name>
</repository>
</repositories>
In this project jetty server plugin is added for the web server. following is the jetty plugin.
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
</configuration>
</plugin>
</plugins>
Then Create a new JSF file and Named as index.xhtml. This is the primary page of this project. And remove the existing index.jsp file.
Creating a Java Persistence Entity Class Representing the Student Table
In this project window, right click the yschool-mini-mayooran project form the contents menu select New---> Entity ClassFollowing New Entity Class window displays. Put the name for class name Student and put the package name org.ymini.yschool.mayooran.entitymodel and then click the Next button.