JPA study

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:

  1. Create a JPA project.

Create a JPA project in eclipse with the name JPAExamples.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值