一對多對映進入集合值關聯的類別,其中實體與其他實體的集合關聯。 因此,在這種型別的關聯中,一個實體的範例可以對映到另一個實體的任意數量的範例。
完整的專案目錄結構如下所示 -
在這個例子中,我們將建立一個學生和圖書庫之間的一對多系,這樣一個學生能發放多種型別的圖書。
這個例子包含以下步驟 -
第1步: 在com.javatpoint.mapping
包下建立一個實體類Student.java
,它包含學生ID(s_id),學生姓名(s_name),其中包含包含List
型別的Library
類物件的@OneToMany
注釋。
檔案:Student.java 的程式碼如下所示 -
package com.yiibai.mapping;
import java.util.List;
import javax.persistence.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int s_id;
private String s_name;
@OneToMany(targetEntity = Library.class)
private List books_issued;
public List getBooks_issued() {
return books_issued;
}
public void setBooks_issued(List books_issued) {
this.books_issued = books_issued;
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
}
第2步: 在com.yiibai.mapping
包下建立另一個實體類Library.java
,其中包含book id(b_id),book name(b_name)以及標記有@OneToMany
注釋的學生型別物件。
檔案:Library.java 的程式碼如下所示 -
package com.yiibai.mapping;
import javax.persistence.*;
@Entity
public class Library {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int b_id;
private String b_name;
public Library(int b_id, String b_name) {
super();
this.b_id = b_id;
this.b_name = b_name;
}
public Library() {
super();
// TODO Auto-generated constructor stub
}
public int getB_id() {
return b_id;
}
public void setB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
}
第3步: 將實體類和其他資料庫組態對映到persistence.xml
檔案中。
檔案: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="book_issued">
<class>com.yiibai.mapping.Student</class>
<class>com.yiibai.mapping.Library</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>
在com.yiibai.onetoone
包下建立一個持久化類OneToManyExample
,用於將實體物件與資料保持一致。
檔案:OneToManyExample.java 的程式碼如下所示 -
package com.yiibai.mapping;
import java.util.ArrayList;
import javax.persistence.*;
import com.yiibai.mapping.Student;
import com.yiibai.mapping.Library;
public class OneToManyExample {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("books_issued");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Library lib1 = new Library();
lib1.setB_id(101);
lib1.setB_name("Data Structure");
Library lib2 = new Library();
lib2.setB_id(102);
lib2.setB_name("DBMS");
em.persist(lib1);
em.persist(lib2);
ArrayList<Library> list = new ArrayList<Library>();
list.add(lib1);
list.add(lib2);
Student st1 = new Student();
st1.setS_id(1);
st1.setS_name("Maxsu");
st1.setBooks_issued(list);
em.persist(st1);
em.getTransaction().commit();
em.close();
emf.close();
}
}
程式執行完成後,下面的表格在MySQL工作台下生成。
select * from student
查詢。結果如下所示 - mysql> select * from student;
+------+--------+
| S_ID | S_NAME |
+------+--------+
| 1 | Maxsu |
+------+--------+
1 row in set
select * from library
查詢。結果如下所示 - mysql> select * from library;
+------+----------------+
| B_ID | B_NAME |
+------+----------------+
| 102 | DBMS |
| 101 | Data Structure |
+------+----------------+
2 rows in set
select * from student_library
查詢。mysql> select * from student_library;
+--------------+-------------------+
| Student_S_ID | books_issued_B_ID |
+--------------+-------------------+
| 1 | 101 |
| 1 | 102 |
+--------------+-------------------+
2 rows in set