JPA(Java Persistence API)学习六(集合映射List)
1.概述
集合(Collection)是一个将多个对象分组为一个单元的java框架。
它用于存储,检索和操作汇总数据。
在JPA中,可以使用集合来持久化包装类和String的对象。
JPA允许三种对象存储在映射集合中 - 基本类型,实体和嵌入式类型。
2.集合类型(根据要求,我们可以使用不同类型的集合来持久化对象)
3.列表(List)
概述:列表(List)是一个用于基于索引插入和删除元素的接口。
当需要以用户定义的顺序检索元素时,可以使用它。
代码示例:
第一步: 创建一个实体类Employee.java
,这个类包含员工id
,name
和嵌入对象(员工地址)。
注解 @ElementCollection
表示嵌入对象。
代码:
import java.util.*;
import javax.persistence.*;
@Entity
public class Employee {
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int e_id;
private String e_name;
@GeneratedValue(strategy = GenerationType.AUTO)
private int e_id;
private String e_name;
@ElementCollection
private List<Address> address = new ArrayList<Address>();
private List<Address> address = new ArrayList<Address>();
}
第二步:创建一个嵌入对象
Address.java
类。 注解@Embeddable
表示可嵌入对象。 import javax.persistence.*;
@Embeddable
public class Address {
public class Address {
private int e_pincode;
private String e_city;
private String e_state;
}
private String e_city;
private String e_state;
}
第三步: 将实体类和数据库配置映射到
persistence.xml
文件中 <?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"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Collection_Type">
<class>com.yiibai.jpa.Employee</class>
<class>com.yiibai.jpa.Address</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
<persistence-unit name="Collection_Type">
<class>com.yiibai.jpa.Employee</class>
<class>com.yiibai.jpa.Address</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
第四步:应用
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Collection_Type");
EntityManager em = emf.createEntityManager();
EntityManager em = emf.createEntityManager();
System.out.println("Start work...");
em.getTransaction().begin();
em.getTransaction().begin();
Address a1 = new Address();
a1.setE_pincode(201301);
a1.setE_city("Guangzhou");
a1.setE_state("GuangDong");
a1.setE_pincode(201301);
a1.setE_city("Guangzhou");
a1.setE_state("GuangDong");
Address a2 = new Address();
a2.setE_pincode(302001);
a2.setE_city("Haikou");
a2.setE_state("Hainan");
a2.setE_pincode(302001);
a2.setE_city("Haikou");
a2.setE_state("Hainan");
Employee e1 = new Employee();
e1.setE_id(1);
e1.setE_name("Maxsu");
e1.getAddress().add(a1);
e1.setE_id(1);
e1.setE_name("Maxsu");
e1.getAddress().add(a1);
Employee e2 = new Employee();
e2.setE_id(2);
e2.setE_name("John");
e2.getAddress().add(a2);
e2.setE_id(2);
e2.setE_name("John");
e2.getAddress().add(a2);
em.persist(e1);
em.persist(e2);
em.persist(e2);
em.getTransaction().commit();
em.close();
emf.close();
System.out.println("End work...");
emf.close();
System.out.println("End work...");
学习来源:https://www.yiibai.com/jpa/jpa-set-mapping.html#article-start