Tuesday, 18 June 2013

JLab3 : Simple Mapping using JPA

Steps in MyEclipse 7.0 :
1. Create a Java Application with name : JLab3
2. Right Click on JLab3 >> MyEclipse >> Add JPA Capabilities
3. Check checkbox 'Hibernate'
4. Check Hibernate Core & Hibernate Annotation checkboxes & click on Next.
5. Provide Persistence Unit name : JLab3PU
6. Click on "Create new Driver"
7. Select 'Driver Template' as 'MySQL Connector/J'
8. Driver Name : mysqldriver
9. Add url, username, password & click on 'Add JARs'
10. Choose DB driver (mysql.jar)
11. Driver classname will automatically populate
12. Check in 'Save Password' checkbox & click on 'Test Driver'
13. Click "OK" >> 'Finish'
14. Click on 'Update List'
15. Select 'Catalog/Schema' as database name from the dropdown menu.
16. Click on 'Finish'.

Directory Structure will update & add all liabraries.
Now, write files, which we need.
1. JPAUtil.java (src/com.jpa.util)
2. persistence.xml (src/META-INF/persistence.xml)
3. Customer.java (src/com.jpa.persistence)
4. Lab3Client.java (src/com.jpa.persistence)

Now, the directory structure will look like as :




















Codes of JLab3 :
1. JPAUtil.java
package com.jpa.util;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
static EntityManagerFactory emf;
static{
try{
emf = Persistence.createEntityManagerFactory("JLab3PU");
}catch(Exception e){
System.out.println("Error in JPAUtil");
e.printStackTrace();
}
}
public static EntityManagerFactory getEntityManagerFactory(){
return emf;
}
}

2. persistence.xml (*NOTE : Will generate automatically, edit in case of need of more Property)
<?xml version="1.0" encoding="UTF-8"?>
<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
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
 
<persistence-unit name="JLab3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
  <properties>
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3333/practice"/>
<property name = "hibernate.connection.username" value = "root"/>
<property name = "hibernate.connection.password" value = "admin"/>
  </properties>
</persistence-unit>
</persistence>

3. Customer.java (Same as ALab2, No need to Edit)
package com.jpa.persistence;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="cust")
public class Customer {
@Id
@GenericGenerator(name="gg",strategy="increment")
@GeneratedValue(generator="gg")
@Column(name="cid")
private int cid;
@Column(name="cname")
private String cname;
public Customer() {
super();
}
public Customer(String cname) {
super();
this.cname = cname;
}
public Customer(int cid, String cname) {
super();
this.cid = cid;
this.cname = cname;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Customer[] cid: "+cid+"cname: "+cname;
}
}

4. Lab3Client.java
package com.jpa.persistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import com.jpa.util.JPAUtil;
public class Lab3Client {
public static void main(String args[]){
EntityTransaction tx = null;
try{
EntityManagerFactory emf = JPAUtil.getEntityManagerFactory();
EntityManager manager = emf.createEntityManager();
tx = manager.getTransaction();
tx.begin();
Customer cust = new Customer("JPA");
manager.persist(cust);
tx.commit();
manager.close();
}catch(Exception e){
e.printStackTrace();
if (tx!=null)
tx.rollback();
}
}
}

RESULT : This will insert a new row in table 'cust'





















* Note : In place of @GeneratedValue, you can use autoincrement feature of database as :
create table cust(cid int primary key auto_increment, cname char(15));


No comments:

Post a Comment