【JPA】第一篇----JPA 一对一映射

·  阅读 45

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

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

JPA @OneToOne一对一映射示例

在此示例中,我们将在学生和图书馆之间创建一对一关系,以便一名学生只能获得一种类型的书籍。

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tengteng</groupId>
    <artifactId>JPAProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
</project>
复制代码

Student实体类

package com.tengteng.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@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;  
    }
}  
复制代码

Library实体类

package com.tengteng.pojo;

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

}  
复制代码

persistence.xml

在resources目录下创建META-INF目录,建立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.tengteng.pojo.Student</class>
        <class>com.tengteng.pojo.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/jpa?serverTimezone=UTC&amp;useSSL=false" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>

</persistence>
复制代码

OneToOneExample测试类

package com.tengteng.test;

import com.tengteng.pojo.Library;
import com.tengteng.pojo.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

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("Jack");
             
           Student st2=new Student();  
           st2.setS_id(2);  
           st2.setS_name("Mark");
             
           em.persist(st1);  
           em.persist(st2);  
             
           Library lib1=new Library();
           lib1.setB_id(101);  
           lib1.setB_name("数据结构");
           lib1.setStud(st1);  
             
           Library lib2=new Library();  
           lib2.setB_id(102);  
           lib2.setB_name("汇编原理");
           lib2.setStud(st2);  
             
           em.persist(lib1);  
           em.persist(lib2);  
             
           em.getTransaction().commit();  
             
           em.close();  
           emf.close();  
    }  
      
      
}  
复制代码

程序执行后,在MySQL下生成了两张表。

  • student表 : 此表包含学生详细信息。要获取数据,请在 MySQL 中运行select * from student查询。

 

  • library表 :此表表示学生和图书馆之间的映射。要获取数据,请在 MySQL 中运行select * from library。

 

总结:这个Jpa的一对一和mybatis的一对一他俩差不多,好好看看!!!

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改