Employee.java
import javax.persistence.*; @Entity @Table(name = "EMPLOYEE") public class Employee { @Id @GeneratedValue @Column(name = "id") private int id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; @Column(name = "salary") private int salary; public Employee() { } public Employee(String fname, String lname, int salary) { this.firstName = fname; this.lastName = lname; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String first_name) { this.firstName = first_name; } public String getLastName() { return lastName; } public void setLastName(String last_name) { this.lastName = last_name; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
resources/META-INF/persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1"> <persistence-unit name="ir.mhdr.jpaDemo" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>Employee</class> <properties> <!-- Configuring JDBC properties --> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/testdb?useUnicode=yes&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="sampad622" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <!-- Hibernate properties --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" /> <property name="hibernate.hbm2ddl.auto" value="create" /> <!-- Configuring Connection Pool --> <property name="hibernate.c3p0.min_size" value="5" /> <property name="hibernate.c3p0.max_size" value="20" /> <property name="hibernate.c3p0.timeout" value="500" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.idle_test_period" value="2000" /> </properties> </persistence-unit> </persistence>
EmployeeBL.java
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.query.criteria.internal.CriteriaUpdateImpl; import javax.persistence.*; import javax.persistence.criteria.CriteriaUpdate; import javax.transaction.Transaction; import java.util.Iterator; import java.util.List; public class EmployeeBL { private EntityManagerFactory factory; public EmployeeBL() { factory = Persistence.createEntityManagerFactory("ir.mhdr.jpaDemo"); } public void close() { factory.close(); } /* Method to CREATE an employee in the database */ public void addEmployee(String fname, String lname, int salary) { EntityManager entityManager = factory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); Employee employee = new Employee(fname, lname, salary); entityManager.persist(employee); tx.commit(); } catch (Exception ex) { if (tx != null) tx.rollback(); ex.printStackTrace(); } finally { entityManager.close(); } } /* Method to READ all the employees */ public void listEmployees() { EntityManager entityManager = factory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); List employees = entityManager.createQuery("from Employee").getResultList(); for (Iterator iterator = employees.iterator(); iterator.hasNext(); ) { Employee employee = (Employee) iterator.next(); System.out.print("First Name: " + employee.getFirstName()); System.out.print(" Last Name: " + employee.getLastName()); System.out.println(" Salary: " + employee.getSalary()); } tx.commit(); } catch (Exception ex) { if (tx != null) tx.rollback(); ex.printStackTrace(); } finally { entityManager.close(); } } /* Method to UPDATE salary for an employee */ public void updateEmployee(Integer EmployeeID, int salary) { EntityManager entityManager = factory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); TypedQuery<Employee> query = entityManager.createQuery("select emp from Employee emp where id=:id", Employee.class); query.setParameter("id", EmployeeID); Employee employee = query.getSingleResult(); employee.setSalary(salary); entityManager.persist(employee); tx.commit(); } catch (Exception ex) { if (tx != null) tx.rollback(); ex.printStackTrace(); } finally { entityManager.close(); } } /* Method to DELETE an employee from the records */ public void deleteEmployee(Integer EmployeeID) { EntityManager entityManager = factory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); Employee employee = entityManager.find(Employee.class, EmployeeID); entityManager.remove(employee); tx.commit(); } catch (Exception ex) { if (tx != null) tx.rollback(); ex.printStackTrace(); } finally { entityManager.close(); } } }
Main.java
public class Main { public static void main(String[] args) { EmployeeBL employeeBL=new EmployeeBL(); employeeBL.addEmployee("Mahmood","Ramzani",1000); employeeBL.close(); } }
References
https://github.com/mhdr/HibernateSamples/tree/master/004_Persistent
http://docs.jboss.org/hibernate/orm/5.2/quickstart/html_single/#tutorial_jpa
https://dzone.com/articles/a-curious-java-language-feature-and-how-it-produce
https://www.sitepoint.com/5-reasons-to-use-jpa-hibernate/