JPA map映射

映射是一个接口, 其中唯一键与每个值对象相关联。因此, 基于关键字执行诸如搜索, 更新, 删除之类的操作。
map映射示例 在此示例中, 我们将对象嵌入实体类中, 并将其定义为集合类型Map。

private Map< Integer, Address> map=new HashMap< Integer, Address> ();

本示例包含以下步骤:-
  • 在com.srcmini.jpa包下创建一个实体类Employee.java, 其中包含员工ID, 姓名和嵌入式对象(员工地址)。注释@ElementCollection表示嵌入式对象。
Employee.java
package com.srcmini.jpa; import java.util.*; import javax.persistence.*; @Entitypublic 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; } }

  • 现在, 在com.srcmini.jpa包下创建一个嵌入式对象Address.java类。批注@Embeddable表示可嵌入对象。
Address.java
package com.srcmini.jpa; import javax.persistence.*; @Embeddablepublic 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; } }

  • 现在, 在Persistence.xml文件中映射实体类和其他数据库配置。
Persistence.xml
< persistence> < persistence-unit name="Collection_Type"> < class> com.srcmini.jpa.Employee< /class> < class> com.srcmini.jpa.Address< /class> < properties> < property name="javax.persistence.jdbc.driver" value="http://www.srcmini.com/com.mysql.jdbc.Driver"/> < property name="javax.persistence.jdbc.url" value="http://www.srcmini.com/jdbc:mysql://localhost:3306/collection_mapping"/> < property name="javax.persistence.jdbc.user" value="http://www.srcmini.com/root"/> < property name="javax.persistence.jdbc.password" value=""/> < property name="eclipselink.logging.level" value="http://www.srcmini.com/SEVERE"/> < property name="eclipselink.ddl-generation" value="http://www.srcmini.com/create-or-extend-tables"/> < /properties> < /persistence-unit> < /persistence>

  • 在com.srcmini.collection包下创建一个持久性类MapMapping.java, 以将实体对象与数据持久化。
MapMapping.java
package com.srcmini.collection; import javax.persistence.*; import com.srcmini.jpa.*; public class ListMapping{ 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("Noida"); a1.setE_state("Uttar Pradesh"); Address a2=new Address(); a2.setE_pincode(302001); a2.setE_city("Jaipur"); a2.setE_state("Rajasthan"); Address a3=new Address(); a3.setE_pincode(133301); a3.setE_city("Chandigarh"); a3.setE_state("Punjab"); Address a4=new Address(); a4.setE_pincode(80001); a4.setE_city("Patna"); a4.setE_state("Bihar"); Employee e1=new Employee(); e1.setE_id(1); e1.setE_name("Vijay"); Employee e2=new Employee(); e2.setE_id(2); e2.setE_name("Vijay"); Employee e3=new Employee(); e3.setE_id(3); e3.setE_name("William"); Employee e4=new Employee(); e4.setE_id(4); e4.setE_name("Rahul"); 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(); } }

【JPA map映射】输出:
程序执行后, 将在MySQL工作台下生成以下表格。
  • 员工表-此表包含员工详细信息。要获取数据, 请在MySQL中从员工查询中运行select *。
JPA map映射

文章图片
  • Employee_map表-此表表示雇员和地址表之间的映射。表中的数据以无序方式排列。要获取数据, 请在MySQL中运行employee *映射查询中的select *。
JPA map映射

文章图片

    推荐阅读