JPA(Java Persistence API)学习七(集合映射Set)
1.概述
集合(Set)是一个包含唯一元素的接口。 这些元素不保留任何顺序。
当需要以无序方式检索唯一元素时,可以使用Set
。
2.示例
第一步:创建一个实体类Employee.java
,这个类包含员工id
,name
和嵌入对象(员工地址)。
import javax.persistence.*;
import java.util.*;
import javax.persistence.*;
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 Set<Address> address = new HashSet<Address>();
private Set<Address> address = new HashSet<Address>();
}
@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-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();
em.getTransaction().begin();
Address a1 = new Address();
a1.setE_pincode(501000);
a1.setE_city("Guangzhou");
a1.setE_state("GuangDong");
a1.setE_pincode(501000);
a1.setE_city("Guangzhou");
a1.setE_state("GuangDong");
Address a2 = new Address();
a2.setE_pincode(571100);
a2.setE_city("Haikou");
a2.setE_state("Hainan");
a2.setE_pincode(571100);
a2.setE_city("Haikou");
a2.setE_state("Hainan");
Address a3 = new Address();
a3.setE_pincode(533300);
a3.setE_city("Hangzhou");
a3.setE_state("Zhejiang");
a3.setE_pincode(533300);
a3.setE_city("Hangzhou");
a3.setE_state("Zhejiang");
Address a4 = new Address();
a4.setE_pincode(780000);
a4.setE_city("Nanjing");
a4.setE_state("Jiangsu");
a4.setE_pincode(780000);
a4.setE_city("Nanjing");
a4.setE_state("Jiangsu");
Employee e1 = new Employee();
e1.setE_id(1000);
e1.setE_name("Maxsu");
e1.setE_id(1000);
e1.setE_name("Maxsu");
Employee e2 = new Employee();
e2.setE_id(1002);
e2.setE_name("Marry");
e2.setE_id(1002);
e2.setE_name("Marry");
Employee e3 = new Employee();
e3.setE_id(1003);
e3.setE_name("William");
e3.setE_id(1003);
e3.setE_name("William");
Employee e4 = new Employee();
e4.setE_id(1004);
e4.setE_name("Curry");
e4.setE_id(1004);
e4.setE_name("Curry");
e1.getAddress().add(a1);
e2.getAddress().add(a2);
e3.getAddress().add(a3);
e4.getAddress().add(a4);
e2.getAddress().add(a2);
e3.getAddress().add(a3);
e4.getAddress().add(a4);
em.persist(e1);
em.persist(e2);
em.persist(e3);
em.persist(e4);
em.persist(e2);
em.persist(e3);
em.persist(e4);
em.getTransaction().commit();
em.close();
emf.close();
emf.close();
学习来源:https://www.yiibai.com/jpa/jpa-set-mapping.html#article-start