Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Use of JPA(Java Persistence API) to implement ORM

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 167
    Comment on it

    JPA:- Java Persistence API is a collection of classes and methods to persist or store data into the database Using the JAVA API.

    The Below code will show you how you can work with JPA.

    1. First we make the POJO.

    package com.evon;
    public class Employee {
           private int id;
           private String name;
           private double salary;
        public int getId() {
            return id;
        public void setId(int id) {
   = id;
        public String getName() {
            return name;
        public void setName(String name) {
   = name;
        public double getSalary() {
            return salary;
        public void setSalary(double salary) {
            this.salary = salary;

    then after we have to create the mapping.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings version="1.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
     <description> XML Mapping file</description>
       <entity class="com.evon.Employee">        
          <table name="EMP"/>
             <id name="id">
                <generated-value strategy="IDENTITY"/>
             <basic name="name">
                <column name="EMP_NAME" length="100"/>
             <basic name="salary">
                 <column name="EMP_SAL" length="100"/>

    create the persistent.xml to specify the configuration like database specific property, connection information and mapping.xml file information etc.

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
        <persistence-unit name="JpaProject">
             <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpadb"/>
             <property name="javax.persistence.jdbc.user" value="root"/>
             <property name="javax.persistence.jdbc.password" value=""/>
             <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
             <property name="eclipselink.logging.level" value="FINE"/>
             <property name="eclipselink.ddl-generation" value="create-tables"/>

    After do all the configuration in persistent.xml and mapping.xml create Class CreateEmployee and run that file.

    package com.evon.service;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import com.evon.Employee;
    public class CreateEmployee {
       public static void main( String[] args ) {
          EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("JpaProject");
          EntityManager entitymanager = emfactory.createEntityManager( );
          entitymanager.getTransaction( ).begin( );
          Employee employee = new Employee( ); 
          employee.setId( 101 );
          employee.setName( "Manish" );
          employee.setSalary( 30000 );
          entitymanager.persist( employee );
          entitymanager.getTransaction( ).commit( );
          entitymanager.close( );
          emfactory.close( );

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: