集合(Set)是一個包含唯一元素的介面。 這些元素不保留任何順序。當需要以無序方式檢索唯一元素時,可以使用Set
。
完整的專案目錄結構如下所示 -
在這個例子中,我們將一個物件嵌入到一個實體類中,並將其定義為一個集合型別 - Set
。
private Set<Address> address=new HashSet<Address>();
這個例子包含以下步驟 -
第1步: 在com.yiibai.jpa
包下建立一個實體類Employee.java
,這個類包含員工id
,name
和嵌入物件(員工地址)。註解 @ElementCollection
表示嵌入物件。
檔案:Employee.java 的程式碼如下所示 -
package com.yiibai.jpa;
import java.util.*;
import javax.persistence.*;
import java.util.*;
import javax.persistence.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int e_id;
private String e_name;
@ElementCollection
private Set<Address> address = new HashSet<Address>();
public int getE_id() {
return e_id;
}
public void setE_id(int e_id) {
this.e_id = e_id;
}
public String getE_name() {
return e_name;
}
public void setE_name(String e_name) {
this.e_name = e_name;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
}
第2步: 在com.yiibai.jpa
包下建立一個嵌入物件Address.java
類。 註解@Embeddable
表示可嵌入物件。
檔案:Address.java 的程式碼如下所示 -
package com.yiibai.jpa;
import javax.persistence.*;
@Embeddable
public class Address {
private int e_pincode;
private String e_city;
private String e_state;
public int getE_pincode() {
return e_pincode;
}
public void setE_pincode(int e_pincode) {
this.e_pincode = e_pincode;
}
public String getE_city() {
return e_city;
}
public void setE_city(String e_city) {
this.e_city = e_city;
}
public String getE_state() {
return e_state;
}
public void setE_state(String e_state) {
this.e_state = e_state;
}
}
第3步: 將實體類和其他資料庫組態對映到persistence.xml
檔案中。
檔案: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 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>
在com.yiibai.collection
包下建立一個持久化類SetMapping.java
,以便將實體物件與資料保持一致。
檔案:SetMapping.java 的程式碼如下所示 -
package com.yiibai.collection;
import javax.persistence.*;
import com.yiibai.jpa.*;
public class SetMapping {
public static void main(String[] args) {
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");
Address a2 = new Address();
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");
Address a4 = new Address();
a4.setE_pincode(780000);
a4.setE_city("Nanjing");
a4.setE_state("Jiangsu");
Employee e1 = new Employee();
e1.setE_id(1000);
e1.setE_name("Maxsu");
Employee e2 = new Employee();
e2.setE_id(1002);
e2.setE_name("Marry");
Employee e3 = new Employee();
e3.setE_id(1003);
e3.setE_name("William");
Employee e4 = new Employee();
e4.setE_id(1004);
e4.setE_name("Curry");
e1.getAddress().add(a1);
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.getTransaction().commit();
em.close();
emf.close();
}
}
程式執行完成後,下面的表格在MySQL工作台下生成。
select * from employee
查詢。結果如下所示 - mysql> select * from employee;
+------+---------+
| E_ID | E_NAME |
+------+---------+
| 1003 | William |
| 1004 | Curry |
| 1000 | Maxsu |
| 1002 | Marry |
+------+---------+
4 rows in set
select * from employee_address
查詢。結果如下所示 - mysql> select * from employee_address;
+-----------+-----------+-----------+---------------+
| E_CITY | E_PINCODE | E_STATE | Employee_E_ID |
+-----------+-----------+-----------+---------------+
| Hangzhou | 533300 | Zhejiang | 1003 |
| Nanjing | 780000 | Jiangsu | 1004 |
| Guangzhou | 501000 | GuangDong | 1000 |
| Haikou | 571100 | Hainan | 1002 |
+-----------+-----------+-----------+---------------+
4 rows in set