JPA多對一映射

多對一映射表示實體集合可以與相似實體關聯的單值關聯。 因此,在關係數據庫中,實體的多於一行可以引用另一個實體的相似行。

完整的項目目錄結構如下所示 -
JPA多對一映射

@ManyToOne 示例

在這個例子中,我們將創建一個學生和圖書庫之間的多對一關係,多個學生可以發佈同一本書。

這個例子包含以下步驟 -

第1步:com.yiibai.mapping包中創建一個實體類Student.java,包含學生ID(s_id)和學生姓名(s_name),其中包含一個包含庫類型對象的[@ManyToOne](https://github.com/ManyToOne "@ManyToOne")註解。

文件:Student.java 的代碼如下所示 -

package com.yiibai.mapping;

import javax.persistence.*;

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int s_id;
    private String s_name;

    @ManyToOne
    private Library lib;

    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;
    }

    public Library getLib() {
        return lib;
    }

    public void setLib(Library lib) {
        this.lib = lib;
    }

}

第2步:com.yiibai.mapping包下創建另一個實體類Library.java,它包含book id(b_id),book name(b_name)。

文件: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.mapping包下創建一個持久化類ManyToOneExample,用於將實體對象與數據保持一致。

文件:ManyToOneExample.java 的代碼如下所示 -

package com.yiibai.mapping;

import javax.persistence.*;

import javax.persistence.EntityManagerFactory;

import com.yiibai.mapping.Library;

public class ManyToOneExample {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("books_issued");

        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        Library lib = new Library();
        lib.setB_id(101);
        lib.setB_name("Data Structure");

        em.persist(lib);

        Student st1 = new Student();
        st1.setS_id(1);
        st1.setS_name("Maxsu");
        st1.setLib(lib);

        Student st2 = new Student();
        st2.setS_id(2);
        st2.setS_name("Curry");
        st2.setLib(lib);

        em.persist(st1);
        ;
        em.persist(st2);

        em.getTransaction().commit();
        em.close();
        emf.close();
    }
}

程序輸出結果

程序執行完成後,下面的表格在MySQL工作臺下生成。

  • Student表 - 此表包含學生詳細信息。要獲取數據,請在MySQL中運行select * from student查詢。結果如下所示 -
mysql> select * from student;
+------+--------+----------+
| S_ID | S_NAME | LIB_B_ID |
+------+--------+----------+
|    2 | Curry  |      101 |
|    1 | Maxsu  |      101 |
+------+--------+----------+
2 rows in set
  • Library表 - 這個表格代表學生和圖書庫之間的映射。 要獲取數據,請在MySQL中運行select * from library查詢。結果如下所示 -
mysql> select * from library;
+------+----------------+
| B_ID | B_NAME         |
+------+----------------+
|  101 | Data Structure |
+------+----------------+
1 row in set