JPA CRUD Example
JPA CRUD Example
3. Employee entity:
Create a class Employee in the package com.thejavageek.jpa.entities and
annotate with @Entity and @Idso that it becomes an entity that is managed
by EntityManager .
Employee entity
Java
package com.thejavageek.jpa.e
import java.io.Serializable;
import javax.persistence.*;
package com.thejavageek.jpa.entities;
2
3
import java.io.Serializable;
import javax.persistence.*;
5
6
@Entity
8
9
@Id
10
11
12
13
14
15
16
17
18
public Employee() {
19
20
21
22
return this.idEmployee;
23
24
25
26
this.idEmployee = idEmployee;
27
28
29
30
return this.email;
31
32
33
34
this.email = email;
35
36
37
38
return this.firstname;
39
40
41
42
this.firstname = firstname;
43
44
45
46
return this.lastname;
47
48
49
50
this.lastname = lastname;
51
52
53
@Override
54
55
56
57
58
59
4. persistence.xml:
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPAExamples">
<class>com.thejavageek.jpa.entities.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/thejavageek" />
<property name="javax.persistence.jdbc.user"
value="yourUserNameForDB" />
<property name="javax.persistence.jdbc.password"
value="yourPasswordForDB" />
</properties>
</persistence-unit>
</persistence>
package com.thejavageek.jpa;
import javax.persistence.EntityM
import javax.persistence.EntityM
package com.thejavageek.jpa;
2
3
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
6
7
import com.thejavageek.jpa.entities.Employee;
8
9
10
11
12
13
/* Create EntityManagerFactory */
14
15
.createEntityManagerFactory("JPAExamples");
16
17
18
19
employee.setFirstname("prasad");
20
employee.setLastname("kharkar");
21
employee.setEmail("[email protected]");
22
employee.setIdEmployee(1);
23
24
/* Create EntityManager */
25
EntityManager em = emf.createEntityManager();
26
27
/* Persist entity */
28
em.getTransaction().begin();
29
em.persist(employee);
30
em.getTransaction().commit();
31
32
/* Retrieve entity */
33
34
System.out.println(employee);
35
36
/* Update entity */
37
em.getTransaction().begin();
38
employee.setFirstname("Pranil");
39
40
em.getTransaction().commit();
41
42
/* Remove entity */
43
em.getTransaction().begin();
44
em.remove(employee);
45
em.getTransaction().commit();
46
47
48
49
50
51
52
Now simply run this program and you will see following output on the console
MS DOS
Employee [idEmployee=1, email=
Employee after updation :- Empl
Employee after removal :- null