Anda di halaman 1dari 7

JPA CRUD example

mongodb update query - July 9, 2015


mongodb find query - July 9, 2015
Hello all, In previous article we have learned about the jpa architecture. This
article will focus on creating a simple JPA CRUD example.
JPA CRUD example:
1. Create a JPA project.
Create a JPA project in eclipse with the name JPAExamples.
2. Create database.
We need some database to store data from jpa entities. Create a database
named thejavageek In our example, We will create a table employee with
columns idemployee, firstname, lastname and email.
Transact-SQL
CREATE TABLE `thejavageek`.
`idemployee` INT NOT NULL ,
`firstname` VARCHAR(45) NUL
`lastname` VARCHAR(45) NULL

CREATE TABLE `thejavageek`.`employee` (

`idemployee` INT NOT NULL ,

`firstname` VARCHAR(45) NULL ,

`lastname` VARCHAR(45) NULL ,

`email` VARCHAR(45) NULL ,

PRIMARY KEY (`idemployee`) );

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

public class Employee implements Serializable {

8
9

@Id

10

private int idEmployee;

11
12

private String email;

13
14

private String firstname;

15
16

private String lastname;

17
18

public Employee() {

19

20
21

public int getIdEmployee() {

22

return this.idEmployee;

23

24
25

public void setIdEmployee(int idEmployee) {

26

this.idEmployee = idEmployee;

27

28
29

public String getEmail() {

30

return this.email;

31

32
33

public void setEmail(String email) {

34

this.email = email;

35

36
37

public String getFirstname() {

38

return this.firstname;

39

40
41

public void setFirstname(String firstname) {

42

this.firstname = firstname;

43

44
45

public String getLastname() {

46

return this.lastname;

47

48
49

public void setLastname(String lastname) {

50

this.lastname = lastname;

51

52
53

@Override

54

public String toString() {

55

return "Employee [idEmployee=" + idEmployee + ", email=" + email

56

+ ", firstname=" + firstname + ", lastname=" + lastname + "]";

57

58
59

4. persistence.xml:

As soon as you created jpa project in eclipse, a file called persistence.xml is


created which stores the information about jpa. Update it as follows. Also, add
the mysql driver jar file into your classpath of project.
XHTML
<?xml version="1.0" encoding="
<persistence version="2.1"
xmlns="http://xmlns.jc
xsi:schemaLocation="

<?xml version="1.0" encoding="UTF-8"?>

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

5. jpa crud example operations:


Now that we have created Employee entity and updated persistence.xml for jpa
configuration, we can perform operations on entities. Create a class Test in
package com.thejavageek.jpa as follows:
Test.java
Java

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

public class Test {

10
11

public static void main(String[] args) {

12
13

/* Create EntityManagerFactory */

14

EntityManagerFactory emf = Persistence

15

.createEntityManagerFactory("JPAExamples");

16
17

/* Create and populate Entity */

18

Employee employee = new Employee();

19

employee.setFirstname("prasad");

20

employee.setLastname("kharkar");

21

employee.setEmail("someMail@gmail.com");

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

employee = em.find(Employee.class, 1);

34

System.out.println(employee);

35
36

/* Update entity */

37

em.getTransaction().begin();

38

employee.setFirstname("Pranil");

39

System.out.println("Employee after updation :- " + employee);

40

em.getTransaction().commit();

41
42

/* Remove entity */

43

em.getTransaction().begin();

44

em.remove(employee);

45

em.getTransaction().commit();

46
47

/* Check whether enittiy is removed or not */

48

employee = em.find(Employee.class, 1);

49

System.out.println("Employee after removal :- " + employee);

50
51

52

Your final project structure would be as follows:

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

1 Employee [idEmployee=1, email=someMail@gmail.com,


firstname=prasad, lastname=kharkar]
2
Employee after updation :- Employee [idEmployee=1,
3
email=someMail@gmail.com, firstname=Pranil, lastname=kharkar]
Employee after removal :- null
I hope this JPA CRUD example helped!!!

Anda mungkin juga menyukai