Set是包含唯一元素的接口。这些元素不维持任何顺序。当需要以无序方式检索唯一元素时, 可以使用Set。
设置映射示例
在此示例中, 我们将对象嵌入实体类中, 并将其定义为集合类型List。
private Set<
Address>
address=new HashSet<
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 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;
} }
- 现在, 在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 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("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.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工作台下生成以下表格。
- 员工表-此表包含员工详细信息。要获取数据, 请在MySQL中从员工查询中运行select *。
文章图片
- Employee_address表-此表表示雇员和地址表之间的映射。表中的数据是无序排列的。要获取数据, 请在MySQL中运行employee * address查询中的select *。
文章图片