Below example on JPA is to enable you to quickly get started with JPA with no extra settings & coding. Further details can be found on internet & I will be giving those later here also. Keep learning & keep smiling :)
First get the jars for hibernate4.0 & include all the jars in the required folder. Rest required jars you have to download separately or you can google for the error which you get in eclipse while executing the code & include that jar then... its simple.
Create the table in database having 2 properties - ID & Name. I have used Oracle database here.
Now come to Eclipse. Create a project with name of your wish & then create the package to create the entity & DAO files like below -
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "PERSON")
public class Person
{
@Id
// @GeneratedValue(strategy = GenerationType.AUTO)
int id;
@Column
String name;
public Person()
{
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now create the persistence.xml in the META-INF folder & place it under 'src' with contents like shown below -
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>entities.Person</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="openjpa.ConnectionDriverName"
value="oracle.jdbc.OracleDriver"/>
<property name="openjpa.ConnectionUserName" value="scott"/>
<property name="openjpa.ConnectionPassword" value="scott"/>
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
Above properties are like common JDBC but different is persistence-unit name="testjpa" which is used to recognize that to which database you want to interact in your code & you can multiple persistence-units each for different database & provider defines how the connections are made for that database. And the classes which will be part of the database.
Now DAO layer for saving the data & fetching the data. First for Saving -
package dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import entities.Person;
public class Saving {
public static void main(String[] args) {
Person cust = null;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
Person person = new Person();
person.setName("Nitin");
person.setId(32);
em.persist(person);
userTransaction.commit();
OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
Object objId = oem.getObjectId(person);
// Below find works only to find those object which were saved or persisted using the same EntityManager
// But this way of fetching during the same flow don't create the new object as shown below by checking equality
cust = em.find(Person.class, objId);
// Below statement also fetch the same person object with EntityManager
// cust = em.find(Person.class, person.getId());
em.close();
entityManagerFactory.close();
System.out.println(cust.getName());
System.out.println(person == cust);
}
}
Now to fetch the data based on the id -
package dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import entities.Person;
public class Fetching {
public static void main(String[] args) {
Person person = null;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
person = em.find(Person.class, 33);
em.close();
entityManagerFactory.close();
System.out.println(person.getName());
}
}
Till here it is working good & this is very simple, having no complexities & I have used jdk 1.7(though not mandatory but you have to use jdk1.5 or above only to use the annotations in the above examples. Later I will provide example having multiple tables with relations among them.
First get the jars for hibernate4.0 & include all the jars in the required folder. Rest required jars you have to download separately or you can google for the error which you get in eclipse while executing the code & include that jar then... its simple.
Create the table in database having 2 properties - ID & Name. I have used Oracle database here.
Now come to Eclipse. Create a project with name of your wish & then create the package to create the entity & DAO files like below -
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "PERSON")
public class Person
{
@Id
// @GeneratedValue(strategy = GenerationType.AUTO)
int id;
@Column
String name;
public Person()
{
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now create the persistence.xml in the META-INF folder & place it under 'src' with contents like shown below -
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>entities.Person</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="openjpa.ConnectionDriverName"
value="oracle.jdbc.OracleDriver"/>
<property name="openjpa.ConnectionUserName" value="scott"/>
<property name="openjpa.ConnectionPassword" value="scott"/>
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
Above properties are like common JDBC but different is persistence-unit name="testjpa" which is used to recognize that to which database you want to interact in your code & you can multiple persistence-units each for different database & provider defines how the connections are made for that database. And the classes which will be part of the database.
Now DAO layer for saving the data & fetching the data. First for Saving -
package dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import entities.Person;
public class Saving {
public static void main(String[] args) {
Person cust = null;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
Person person = new Person();
person.setName("Nitin");
person.setId(32);
em.persist(person);
userTransaction.commit();
OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
Object objId = oem.getObjectId(person);
// Below find works only to find those object which were saved or persisted using the same EntityManager
// But this way of fetching during the same flow don't create the new object as shown below by checking equality
cust = em.find(Person.class, objId);
// Below statement also fetch the same person object with EntityManager
// cust = em.find(Person.class, person.getId());
em.close();
entityManagerFactory.close();
System.out.println(cust.getName());
System.out.println(person == cust);
}
}
Now to fetch the data based on the id -
package dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import entities.Person;
public class Fetching {
public static void main(String[] args) {
Person person = null;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
person = em.find(Person.class, 33);
em.close();
entityManagerFactory.close();
System.out.println(person.getName());
}
}
Till here it is working good & this is very simple, having no complexities & I have used jdk 1.7(though not mandatory but you have to use jdk1.5 or above only to use the annotations in the above examples. Later I will provide example having multiple tables with relations among them.