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.

    Employee.java

    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) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.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="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
    
     <description> XML Mapping file</description>
    
       <entity class="com.evon.Employee">        
          <table name="EMP"/>
          <attributes>
    
             <id name="id">
                <generated-value strategy="IDENTITY"/>
             </id>
    
             <basic name="name">
                <column name="EMP_NAME" length="100"/>
             </basic>
    
             <basic name="salary">
                 <column name="EMP_SAL" length="100"/>
             </basic>
    
    
    
          </attributes>
       </entity>
    
    </entity-mappings>
    

    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="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">
        <persistence-unit name="JpaProject">
            <mapping-file>META-INF/mapping.xml</mapping-file>
    
            <class>com.evon.Employee</class>
    
          <properties>
             <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"/>
          </properties>
    
        </persistence-unit>
    </persistence>
    

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

    CreateEmployee.java

    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: