JPA 2.HelloWorld
JPA
@Author:hanguixian
@Email:hn_hanguixian@163.com
二 JPA HelloWorld
使用JPA持久化对象的步骤
1.创建 persistence.xml, 在这个文件中配置持久化单元
需要指定跟哪个数据库进行交互;
需要指定 JPA 使用哪个持久化的框架以及配置该框架的基本属性
2.创建实体类, 使用 annotation 来描述实体类跟数据库表之间的映射关系.
3.使用 JPA API 完成数据增加、删除、修改和查询操作
创建 EntityManagerFactory (对应 Hibernate 中的 SessionFactory)
创建 EntityManager (对应 Hibernate 中的Session)
在IntelliJ IDEA 下,创建JPA的Maven工程
- pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ifox.hgx</groupId> <artifactId>jpa_m2</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.3.1.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.3.1.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
persistence.xml
JPA 规范要求在类路径的 META-INF 目录下放置persistence.xml,文件的名称是固定的
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="jpa_m2" transaction-type="RESOURCE_LOCAL"> <!--配置使用什么 ORM 产品来作为 JPA 的实现 --> <!--5.2版本以后,无法使用HibernatePersistence--> <!--如果JPA项目中只有一个实现产品,可以配置不这个节点--> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <!--添加持久化类--> <class>jpa.entity.Customer</class> <properties> <!-- 连接数据库的基本信息 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="123456"/> <!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 --> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
实体类:Customer
package jpa.entity; import javax.persistence.*; @Table(name = "JPA_CUSTOMTERS") @Entity public class Customer { private Integer id ; private String lastName ; private String email ; private Integer age ; // GeneratedValue 生成方式:策略为 GenerationType.AUTO 自动选择 @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "LAST_NAME") public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
测试:
package jpa; import jpa.entity.Customer; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class TestHello { public static void main(String[] args) { //1. 创建 EntityManagerFactory //注意此处需要和persistence.xml下的persistence-unit name值相同 String persistenceUnitName = "jpa_m2"; EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory EntityManager entityManager = entityManagerFactory.createEntityManager(); //3. 开启事务 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); //4. 进行持久化操作 Customer customer = new Customer(); customer.setAge(12); customer.setEmail("tom@153.com"); customer.setLastName("Tom"); entityManager.persist(customer); //5. 提交事务 transaction.commit(); //6. 关闭 EntityManager entityManager.close(); //7. 关闭 EntityManagerFactory entityManagerFactory.close(); } }