Monday, January 11, 2010

Creating a Simple POJO file from database table-Use of JAVA persistence API

Go to jdeveloper application navigator select the application server and right click new application.

Provide some logical name to the application,provide some package name and in the application template choose web application as shown below and say ok.




Now if you will see two folders are created wihitn the application





Now select model and right click on new



the following window will come up




Now select EJB in the left hand panel and Entities from tables in right hand panel and say ok.Again the following screen will come up





say next and choose the database schema you have created.




Say next and following screen will come up there in click on query



It will show the arpit table



Choose the arpit table and move it to the right side by clicking on the right arrow button and say next find out the following screen next




say next



say next and say finish





Now you can find that a entity bean Arpit.java is created along with the deployment descriptor persistence.xml







AS you can see in the code all the variable that are there in the table arpit has been already included along with the getter and setter methods.

You can also find the following in the program

@Entity
@NamedQuery(name = "Arpit1.findAll", query = "select o from Arpit1 o")
@Table(name = "ARPIT")

This is the new feature in java called as annotation.Which allows to persist the data in the database.AS you can see in the query we are selecting an object hence all the classes has to implement the serializable interface.

No comments: