top of page

EclipseLink Assignment Help

Updated: Oct 22, 2021

Hi, Hope you are doing well. This post is focused on EclipseLink.

Let Start with an introduction of EclipseLink.


What is EclipseLink?


EclipseLink is an open source Eclipse Persistence Services Project from the Eclipse Foundation. The software provides an extensible framework that allows Java developers to interact with various data services, including databases, web services, Object XML mapping, and Enterprise Information Systems. EclipseLink is based on the TopLink product from which Oracle contributed the source code to create the EclipseLink project

EclipseLink is reference implementation for the Java Persistence API (JPA) specification

Creating a Sample Project Using JPA and EclipseLink


I am creating a sample project that persists Employee object into database using JPA


Step 1. Firstly create a Java Project (File->New->Project), select Java Project and click next, provide name as "EmpExample"

persistence.xml

persistence.xml file must be under src/META-INF


<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_2_0.xsd"   version="2.0" 
xmlns="http://java.sun.com/xml/ns/persistence"><persistence-unit name="test" transaction-type="RESOURCE_LOCAL"><class>com.test.jpa.Employee</class><properties><property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" /><property name="javax.persistence.jdbc.url"    value="jdbc:oracle:thin:@192.148.27.140:1521:database" /><property name="javax.persistence.jdbc.user" value="root" /><property name="javax.persistence.jdbc.password" value="pass" /><property name="eclipselink.ddl-generation" value="create-tables" /><property name="eclipselink.ddl-generation.output-mode" value="database" /></properties>
</persistence-unit>
</persistence>

Create a Employee Object(Employee.java)


package com.test.jpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Employee")
public class Employee implements java.io.Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "EmpID")
  private long empId;
  @Column(name = "EmpNAME")
  private String empName;

  public void setEmpId(long empId) {
    this.empId = empId;
  }

  public long getEmpId() {
    return empId;
  }

  public void setEmpName(String empName) {
    this.empName =empName;
  }

  public String getEmpName() {
    return empName;
  }

}

@Entity declares the class as an entity (i.e. a persistent POJO class) @Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity. @Id declares the identifier property of this entity. @GeneratedValue annotation is used to specify the primary key generation strategy to use. If the strategy is not specified by default AUTO will be used. @Column annotation is used to specify the details of the column to which a field or property will be mapped. If the @Column annotation is not specified by default the property name will be used as the column name.


EntityManagerUtil.java


package com.test.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EntityManagerUtil {
  private static final EntityManagerFactory entityManagerFactory;
  static {
    try {
      entityManagerFactory = Persistence.createEntityManagerFactory("test");

    } catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static EntityManager getEntityManager() {
    return entityManagerFactory.createEntityManager();

  }
}

Testing (EmpExample.java)


package com.test.jpa;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;



public class EmpExample {

  private EntityManager entityManager = EntityManagerUtil.getEntityManager();

  public static void main(String[] args) {
    EmpExample example = new EmpExample();
    System.out.println("After Sucessfully insertion ");
    Employee emp1 = example.saveEmployee("Sumith");
    Employee emp2 = example.saveEmployee("Anoop");
    example.listEmployee();
    System.out.println("After Sucessfully modification ");
    example.updateStudent(emp1.getEmpId(), "Sumith Honai");
    example.updateStudent(emp2.getEmpId(), "Anoop Pavanai");
    example.listEmployee();
    System.out.println("After Sucessfully deletion ");
    example.deleteEmployee(emp2.getEmpId());
    example.listEmployee();
   

  }

  public Employee saveEmployee(String empName) {
    Employee emp = new Employee();
    try {
      entityManager.getTransaction().begin();
      emp.setEmpName(empName);
      emp = entityManager.merge(emp);
      entityManager.getTransaction().commit();
    } catch (Exception e) {
      entityManager.getTransaction().rollback();
    }
    return emp;
  }

  public void listEmployee() {
    try {
      entityManager.getTransaction().begin();
      @SuppressWarnings("unchecked")
      List<Employee> Employees = entityManager.createQuery("from Employee").getResultList();
      for (Iterator<Employee> iterator = Employees.iterator(); iterator.hasNext();) {
        Employee emp = (Employee) iterator.next();
        System.out.println(emp.getEmpName());
      }
      entityManager.getTransaction().commit();
    } catch (Exception e) {
      entityManager.getTransaction().rollback();
    }
  }

  public void updateEmployee(Long empId, String empName) {
    try {
      entityManager.getTransaction().begin();
      Employee emp = (Employee) entityManager.find(Employee.class, empId);
      emp.setEmpName(empName);
      entityManager.getTransaction().commit();
    } catch (Exception e) {
      entityManager.getTransaction().rollback();
    }
  }

  public void deleteEmployee(Long empId) {
    try {
      entityManager.getTransaction().begin();
      Employee emp = (Employee) entityManager.find(Employee.class, empId);
      entityManager.remove(emp);
      entityManager.getTransaction().commit();
    } catch (Exception e) {
      entityManager.getTransaction().rollback();
    }
  }
}

Output


After Successfully insertion 
Sumith Anoop 
After Successfully modification  
Sumith Honai Anoop Pavanai 
After Successfully deletion  
Sumith Honai

How CodersArts can help you in EclipseLink?

Codersarts provide

  • EclipseLink Assignment Help

  • Mentorship from Expert

  • Error Resolving

  • Development Help in EclipseLink Project

If you are looking for any kind of help in EcliseLink,Contact us for instant Help.
bottom of page