JPA Set映射
Set 是一个存入不重复元素的接口。这些元素不保证任何顺序。当需要以无序方式检索唯一元素时,可以使用 Set。
JPA Set映射示例
项目结构如下:
在这个例子中,我们将一个对象嵌入到一个实体类中,并将其定义为一个集合类型 Set。
private Set<Address> address= new HashSet<Address>();
pom依赖
<?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.yiidian</groupId>
<artifactId>JPAProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
Address实体类
package com.yiidian.pojo;
import javax.persistence.Embeddable;
@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;
}
}
Employee实体类
package com.yiidian.pojo;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@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;
}
}
persistence.xml
在resources目录下创建META-INF目录,建立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.yiidian.pojo.Address</class>
<class>com.yiidian.pojo.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC&useSSL=false" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
SetMapping测试类
package com.yiidian.test;
import com.yiidian.pojo.Address;
import com.yiidian.pojo.Employee;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
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(201301);
a1.setE_city("广州");
a1.setE_state("天河区");
Address a2=new Address();
a2.setE_pincode(302001);
a2.setE_city("广州");
a2.setE_state("番禺区");
Address a3=new Address();
a3.setE_pincode(133301);
a3.setE_city("广州");
a3.setE_state("越秀区");
Address a4=new Address();
a4.setE_pincode(80001);
a4.setE_city("广州");
a4.setE_state("黄埔区");
Employee e1=new Employee();
e1.setE_id(1);
e1.setE_name("Mark");
Employee e2=new Employee();
e2.setE_id(2);
e2.setE_name("Jack");
Employee e3=new Employee();
e3.setE_id(3);
e3.setE_name("Rose");
Employee e4=new Employee();
e4.setE_id(4);
e4.setE_name("Lucy");
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下生成下表。
- employee表 :此表包含员工详细信息。要获取数据,请在 MySQL 中运行 select * from employee 查询。
- employee_address 表 : 该表表示员工和地址表之间的映射。要获取数据,请在 MySQL 中运行 seelect * from employee_address 查询。
热门文章
优秀文章