http://www.objectdb.com/java/jpa/persistence/store
http://www.objectdb.com/java/jpa/persistence/overview
http://www.thejavageek.com/2014/01/12/jpa-crud-example/
http://www.thoughts-on-java.org/call-stored-procedures-jpa/
http://stackoverflow.com/questions/3572626/calling-stored-procedure-from-java-jpa
http://www.ibm.com/developerworks/cn/aix/library/au-vitips.html
Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment.
JPA CRUD example:
- Create a JPA project.
Create a JPA project in eclipse with the name JPAExamples.
- 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.
1
2
3
4
5
6
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 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.thejavageek.jpa.entities;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Employee implements Serializable {
@Id
private int idEmployee;
private String email;
private String firstname;
private String lastname;
public Employee() {
}
public int getIdEmployee() {
return this.idEmployee;
}
public void setIdEmployee(int idEmployee) {
this.idEmployee = idEmployee;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return "Employee [idEmployee=" + idEmployee + ", email=" + email
+ ", firstname=" + firstname + ", lastname=" + lastname + "]";
}
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14