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 Userinfo.java 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.
Userinfo.java
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Userinfo implements Serializable {
@Id
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 this.id; }
public void setId(int id) { this.id = id; }
public String getEmail() { return this.email; }
public void setEmail(String email) { this.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 this.name; }
public void setName(String name) { this.name = name; }
}
Client.java
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.setId(2);
user.setEmail("binod@abc.com");
user.setFullname("Binod Suman");
user.setPassword("minmax");
user.setName("Binod");
entityManager.persist(user);
entityManager.getTransaction().commit(); }
}
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0"> <persistence-unit name="sumanunit" transaction-type="RESOURCE_LOCAL"> <provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>Userinfo</class>
<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" />
</properties>
</persistence-unit>
</persistence>
compile the code and see the database with one new entry. :)
Saturday, October 3, 2009
Subscribe to:
Post Comments (Atom)
Hi Suman,
ReplyDeleteI 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.
hi i try your code but i am not able to run the JPADEMO.
ReplyDeletePlease guide me.
How to run the project?
ReplyDelete