Saturday, October 3, 2009

JPA example outside the container, jpa standalone application example

One simple JPA example outside the container

1. Create one java project in Eclipse (Say JAPDEMO)
2. Put toplink-essentials.jar and postgresql-8.1dev-403.jdbc2ee.jar in project (into classpath)
3. put into JAPDEMO\src folder
4. put Client. java into JAPDEMO\src folder
5. put persistence.xml into JAPDEMO\src\META-INF folder.
6. Setup a database and make sure you can access it with the correct given information. I used Postgres SQL and I have put the jar for this database.I also used a simple "userinfo" table for my testing, columns: id, name, fullName, passwordinfo, email.
import javax.persistence.Entity;
import javax.persistence.Id;

public class Userinfo implements Serializable {
private int id;
private String email;
private String fullname;
private String passwordinfo;
private String name;
private static final long serialVersionUID = 1L;

public Userinfo() { super(); }
public int getId() { return; }
public void setId(int id) { = id; }
public String getEmail() { return; }
public void setEmail(String email) { = email; }
public String getFullname() { return this.fullname; }
public void setFullname(String fullname) { this.fullname = fullname; }
public String getPassword() { return this.passwordinfo; }
public void setPassword(String password) { this.passwordinfo = password; }
public String getName() { return; }
public void setName(String name) { = name; }
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import oracle.toplink.essentials.ejb.cmp3.EntityManager;

public class Client {
public static void main(String[] args) {
EntityManagerFactory emf; emf = Persistence.createEntityManagerFactory("sumanunit", new java.util.HashMap());
EntityManager entityManager = (EntityManager) emf.createEntityManager(); entityManager.getTransaction().begin();
Userinfo user = new Userinfo();
user.setFullname("Binod Suman");
entityManager.getTransaction().commit(); }

<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation=" persistence_1_0.xsd" version="1.0"> <persistence-unit name="sumanunit" transaction-type="RESOURCE_LOCAL"> <provider>oracle.toplink.essentials.PersistenceProvider</provider>
<properties> <property name="toplink.logging.level" value="FINEST" />
<property name="toplink.jdbc.driver" value="org.postgresql.Driver" />
<property name="toplink.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="toplink.jdbc.user" value="postgres" />
<property name="toplink.jdbc.password" value="suman" />

compile the code and see the database with one new entry. :)


  1. Hi Suman,

    I appreciate your Java complicated technologies with simple examples to undersatnd. Would you please provide me a simple Hibernate and I batis example for CRUD. Thanks in advance.

  2. hi i try your code but i am not able to run the JPADEMO.
    Please guide me.

  3. How to run the project?


You can put your comments here (Either feedback or your Question related to blog)