Share on Google+Share on Google+

Creating and running the JPA CRUD application

In this section we will show you how to develop different project artifacts for JPA CRUD demo example code.

Creating and running the JPA CRUD application


In this section we will show you how to develop different project artifacts for JPA CRUD demo example code.

After completing the steps discussed in this section you will able to create and execute the CRUD demo application.

Following the following steps to create the JPA CRUD demo application.

Creating and setting up the database

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(40) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) default NULL, 

Add the jar files discussed in the previous section in the project library.

Persistence Manager configuration (persistence.xml)

<?xml version="1.0" encoding="UTF-8"?>


<persistence-unit name="jpa">




<property name="hibernate.connection.url" value="jdbc:mysql://"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

<property name="hibernate.connection.password" value="root"/>

<property name="hibernate.connection.username" value="root"/>

<property name="" value="update"/>

<property name="hibernate.show_sql" value="true"/>


<property name="hibernate.format_sql" value="true"/>





Create a "" file

Put the following text:

Creating Model ""

package roseindia;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
public class Student {
private int id;
* @return the id
public int getId() {
return id;
* @param id the id to set
public void setId(int id) { = id;
@Column(name="sname", length=100,nullable=false)
private String sname;
* @return the sname
public String getSname() {
return sname;
* @param sname the sname to set
public void setSname(String sname) {
this.sname = sname;
private int sroll;
* @return the sroll
public int getSroll() {
return sroll;
* @param sroll the sroll to set
public void setSroll(int sroll) {
this.sroll = sroll;
private String scourse;
* @return the scourse
public String getScourse() {
return scourse;
* @param scourse the scourse to set
public void setScourse(String scourse) {
this.scourse = scourse;

Create a "JPACreate" class file

Put the following text in "" file.

Creating Add code and testing the add code. ""

package roseindia;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import roseindia.Student;

* @author Administrator
public class JPACreate {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
Student st = new Student();
st.setSname("Vinod Kumar");
catch(Exception e){


To run this application. Go Run menu and select Open Run Dialog...

Open "Run" dialog box on the screen. Select your project and main class file.

Click on "Run" button. You get an output in "Console window" of eclipse.

Go in mysql and check.



Posted on: April 3, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Creating and running the JPA CRUD application   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image