JPA一对一映射

一对一映射映射表示一个单值关联,其中一个实体的实例与另一个实体的实例相关联。在这种类型的关联中,源实体的一个实例可以映射到目标实体的一个实例。

完整的项目目录结构如下所示 -

@OneToOne示例

在这个例子中,我们将创建一个学生和图书库之间的一对一关系,这样一个学生只能发放一种类型的图书。

这个例子包含以下步骤 -

第1步:com.zyiz.jpa包下创建一个实体类Employee.java,在com.zyiz.mapping包下创建一个实体类Student.java,含学生ID(s_id)和学生姓名(s_name)。

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

package com.zyiz.mapping;

import javax.persistence.*;

@Entity
public class Student {

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

    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.zyiz.mapping包下创建另一个实体类Library.java,其中包含book id(b_id),book name(b_name)以及标记有@OneToOne注释的学生类型对象。

文件:Library.java 的代码如下所示 -

package com.zyiz.mapping;

import javax.persistence.*;

@Entity
public class Library {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int b_id;
    private String b_name;

    @OneToOne
    private Student stud;

    public Library(int b_id, String b_name, Student stud) {
        super();
        this.b_id = b_id;
        this.b_name = b_name;
        this.stud = stud;
    }

    public Library() {
        super();

    }

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

    public Student getStud() {
        return stud;
    }

    public void setStud(Student stud) {
        this.stud = stud;
    }

}

第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.zyiz.mapping.Student</class>
        <class>com.zyiz.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.zyiz.onetoone包下创建一个持久化类OneToOneExample,用于将实体对象与数据保持一致。

文件:OneToOneExample.java 的代码如下所示 -

package com.zyiz.mapping;

import javax.persistence.*;  
import com.zyiz.mapping.*;

public class OneToOneExample {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Book_issued");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

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

        Student st2 = new Student();
        st2.setS_id(2);
        st2.setS_name("James");

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

        Library lib1 = new Library();
        lib1.setB_id(101);
        lib1.setB_name("Data Structure");
        lib1.setStud(st1);

        Library lib2 = new Library();
        lib2.setB_id(102);
        lib2.setB_name("DBMS");
        lib2.setStud(st2);

        em.persist(lib1);
        em.persist(lib2);

        em.getTransaction().commit();

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

}

程序输出结果

程序执行完成后,下面的表格在MySQL工作台下生成。

  • Student表 - 此表包含学生详细信息。要获取数据,请在MySQL中运行select * from student查询。结果如下所示 -
mysql> select * from student;
+------+-------+--------+
| S_ID | S_AGE | S_NAME |
+------+-------+--------+
|    2 | NULL  | James  |
|    1 | NULL  | Maxsu  |
+------+-------+--------+
rows in set
  • Library表 - 这个表格代表学生和图书库之间的映射。 要获取数据,请在MySQL中运行select * from library查询。结果如下所示 -
mysql> select * from library;
+------+----------------+-----------+
| B_ID | B_NAME         | STUD_S_ID |
+------+----------------+-----------+
|  102 | DBMS           |         2 |
|  101 | Data Structure |         1 |
+------+----------------+-----------+
rows in set

上一篇:JPA Map映射

下一篇:JPA一对多映射

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程