列表是一个接口, 用于根据索引插入和删除元素。当需要按用户定义的顺序检索元素时, 可以使用它。
列表映射示例
【JPA列表映射】在此示例中, 我们将对象嵌入实体类中, 并将其定义为集合类型List。
private List<
Address>
address=new ArrayList<
Address>
();
本示例包含以下步骤:-
- 在com.srcmini.jpa包下创建一个实体类Employee.java, 其中包含员工ID, 姓名和嵌入式对象(员工地址)。注释@ElementCollection表示嵌入式对象。
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 List<
Address>
address=new ArrayList<
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 List<
Address>
getAddress() {return address;
} public void setAddress(List<
Address>
address) {this.address = address;
} }
- 现在, 在com.srcmini.jpa包下创建一个嵌入式对象Address.java类。批注@Embeddable表示可嵌入对象。
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>
<
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包下创建一个持久性类ListMapping.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");
Employee e1=new Employee();
e1.setE_id(1);
e1.setE_name("Vijay");
e1.getAddress().add(a1);
Employee e2=new Employee();
e2.setE_id(2);
e2.setE_name("John");
e2.getAddress().add(a2);
em.persist(e1);
em.persist(e2);
em.getTransaction().commit();
em.close();
emf.close();
} }
输出:
程序执行后, 将在MySQL工作台下生成下表。
- 员工表-此表包含员工详细信息。要获取数据, 请在MySQL中从员工查询中运行select *。
文章图片
- Employee_address表-此表表示雇员和地址表之间的映射。要获取数据, 请在MySQL中运行select * from employee_address查询。
文章图片