JPA(Java Persistence API)学习八(集合映射Map)

 1.概述

   Map是一个接口,其中一个唯一键与每个值对象相关联。

   因此,搜索,更新,删除等操作都是基于键来进行的。

 

2.示例

   第一步:

        概述:创建一个实体类Employee.java,这个类包含员工idname 和嵌入对象(员工地址)。

                   注解 @ElementCollection 表示嵌入对象。

         代码:

                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 Map<Integer, Address> map = new HashMap<Integer, Address>();
             }
     第二步:创建一个嵌入对象Address.java类。 注解@Embeddable表示可嵌入对象。
 
             import javax.persistence.*;
 
             @Embeddable
             public class Address {
                 private int e_pincode;
                 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 "
                                                     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>
 
       第四步:应用          
             EntityManagerFactory emf = Persistence.createEntityManagerFactory("Collection_Type");
             EntityManager em = emf.createEntityManager();
             em.getTransaction().begin();
             Address a1 = new Address();
             a1.setE_pincode(511000);
             a1.setE_city("Guangzhou");
             a1.setE_state("Guangdong");
             Address a2 = new Address();
             a2.setE_pincode(202001);
             a2.setE_city("Nanjing");
             a2.setE_state("Jiangsu");
             Address a3 = new Address();
             a3.setE_pincode(333301);
             a3.setE_city("Chengdu");
             a3.setE_state("Shichuan");
             Address a4 = new Address();
             a4.setE_pincode(80001);
             a4.setE_city("Haikou");
             a4.setE_state("Hainan");
             Employee e1 = new Employee();
             e1.setE_id(1);
             e1.setE_name("Maxsu");
             Employee e2 = new Employee();
             e2.setE_id(2);
             e2.setE_name("Leeze");
             Employee e3 = new Employee();
             e3.setE_id(3);
             e3.setE_name("William");
             Employee e4 = new Employee();
             e4.setE_id(4);
             e4.setE_name("Curry");
             e1.getMap().put(1, a1);
             e2.getMap().put(2, a2);
             e3.getMap().put(3, a3);
             e4.getMap().put(4, a4);
             em.persist(e1);
             em.persist(e2);
             em.persist(e3);
             em.persist(e4);
             em.getTransaction().commit();
             em.close();
             emf.close();
 
学习来源:https://www.yiibai.com/jpa/jpa-map-mapping.html#article-start
 
posted @ 2020-09-08 13:36  小窝蜗  阅读(315)  评论(0)    收藏  举报