What is Java Persistence API? In this post I describe You it step by step in practical project. You must early create database with several tables. I create mine in SQL Server 2012 . My database of test name has tables: person, prof, pp and authors. It is simple diagram these tables with realations:
First you must open Netbeans to add driver of SQL Server to the IDE. You find how do it in my post:
How add SQL Server 2012 driver to Netbeans 8?
You must either set connection to the database. I described it in post:
Add connection in Netbeans 8 to the SQL Server 2012 database.
In this moment You may create new project. Choose from menu File->New project.
In New Project window select from Categories section Java Web and from Project section Web Application.
Click the Next button. In Project Name write MyNewJPA as name of your project.
In next step stay default options and click the Next button.
In New Web Application wizard select JavaServer Faces framework.
Click the Finish button. In Projects window You see tree of your project.
Check if you have definition of connection to database.
Right click on the project name and choose from Categories section Persistence and from File Types section Database Schema.
Click the Next button. In File Name field write mySchemaSQLServer and click the Next button.
In New File wizard select list and choose connection to your database.
Click the Next button. You can see available tables from database.
Select any tables and click the Add button.
These tables will move on the right side.
In the Projects window you see files your project.
Right click the name of project and choose New and Others.
From Categories select Persistence and from File Types choose Entity Classes from Database.
Click the Next button. You see window New Entity Classes from Database.
Click the Add All button.
The tables be move on the right. Click the Next button.
In Package write net.doraprojects. Click the Next button.
Check Full Qualified Database … box and click the Finish button.
In Projects window you see new files.
In persistence.xml file you see definition of default data source.
Then you may create pages for tables class. Right click the project node and choose New->Other.
In Categories choose Persistence and from File Types select JSF Pages.
Click the Next button. In Available Entity Classes you list of table classes. Click the Add All button.
These class move to Selected Entity Classes section.
Click the Next button. In Session Bean Package field write net.doraprojects.controllers and in JSF Classes Package field write net.doraprojects.pages. Check Override existing files box.
Click the Finish button.
It is project tree with new files:
The index.xhtml file was been overwritten.
If you run application you see website like this:
If click the Show All Person link you see list of persons from test database from Person table.