1 entityManager.getTransaction().begin();
2 entityManager.remove(emp);
3 entityManager.getTransaction().commit();
4
1EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
2 EntityManager entityManager = entityManagerFactory.createEntityManager();
3
4 entityManager.getTransaction().begin();
5 Employee employee = new Employee();
6 employee.setName("Sanjay");
7
8 // Save Entity
9 entityManager.persist(employee);
10 entityManager.getTransaction().commit();
11 System.out.println("Generated Employee ID = " + employee.getEmployeeId());
12
1<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
4 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
5 version="2.1">
6
7 <persistence-unit name="persistence">
8 <description>JPA Entity Manager</description>
9 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
10
11 <properties>
12 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
13 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
14 <property name="hibernate.hbm2ddl.auto" value="update" />
15 <property name="javax.persistence.jdbc.user" value="root" />
16 <property name="javax.persistence.jdbc.password" value="root" />
17 <property name="hibernate.show_sql" value="true" />
18 </properties>
19
20 </persistence-unit>
21
22</persistence>
23
1package com.frugalis.entity;
2
3import javax.persistence.Column;
4import javax.persistence.Entity;
5import javax.persistence.GeneratedValue;
6import javax.persistence.GenerationType;
7import javax.persistence.Id;
8import javax.persistence.Table;
9
10@Entity
11@Table(name = "EMPLOYEE")
12public class Employee {
13 private int employeeId;
14
15 private String name;
16
17 @Id
18 @Column(name = "EMPLOYEE_ID")
19 @GeneratedValue(strategy = GenerationType.IDENTITY)
20 public int getEmployeeId() {
21 return employeeId;
22 }
23
24 public void setEmployeeId(int employeeId) {
25 this.employeeId = employeeId;
26 }
27
28 @Column(name = "EMPLOYEE_NAME")
29 public String getName() {
30 return name;
31 }
32
33 public void setName(String name) {
34 this.name = name;
35 }
36
37 @Override
38 public String toString() {
39 return "Employee [ID=" + employeeId + ", Name=" + name + "]";
40 }
41
42}
43