JPA Map映射
Map 是一个接口,其中唯一的键与每个值对象相关联。这样,查询、更新、删除等操作都是在key的基础上进行的。
JPA Map映射示例
项目结构如下:
在这个例子中,我们将一个对象嵌入到一个实体类中,并将其定义为集合类型 Map。
private Map<Integer,Address> map= new HashMap<Integer,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.HashMap;
import java.util.Map;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int e_id;
private String e_name;
@ElementCollection
private Map<Integer,Address> map=new HashMap<Integer,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 Map<Integer, Address> getMap() {
return map;
}
public void setMap(Map<Integer, Address> map) {
this.map = map;
}
}
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>
MapMapping测试类
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 MapMapping {
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("番禺区");
Employee e1=new Employee();
e1.setE_id(1);
e1.setE_name("Jack");
e1.getMap().put(1001,a1);
Employee e2=new Employee();
e2.setE_id(2);
e2.setE_name("Rose");
e1.getMap().put(1002,a2);
em.persist(e1);
em.persist(e2);
em.getTransaction().commit();
em.close();
emf.close();
}
}
程序执行后,在MySQL下生成下表。
- employee表 :此表包含员工详细信息。要获取数据,请在 MySQL 中运行 select * from employee 查询。
- employee_map表 : 该表表示员工和地址表之间的映射。要获取数据,请在 MySQL 中运行 seelect * from employee_map 查询。
热门文章
优秀文章