(转载)Jpa配置一对多关系

(转载)Jpa配置一对多关系链接: https://www.cnblogs.com/a-small-lyf/p/10699326.html 在网上查了很多关于jpa的一对多表关联的操作,踩了很多坑,今天终于解决了 下面上一下我…

(转载)Jpa配置一对多关系

链接: https://www.cnblogs.com/a-small-lyf/p/10699326.html

 

在网上查了很多关于jpa的一对多表关联的操作,踩了很多坑,今天终于解决了

下面上一下我自己的代码,记录一下

老师和学生(一对多关系)

首先搭建环境,添加依赖包

复制代码
<?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.lyf</groupId>
    <artifactId>one-to-more</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
    </dependencies>
</project>

代码100分

复制代码

编写数据库配置文件

复制代码
代码100分spring.datasource.url=jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=08186912
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.database=mysql
复制代码

实体类

复制代码
package com.lyf.pojo;

import lombok.Data;

import javax.persistence.*;

/**
 * @Date:2019-04-12
 * @Description:com.lyf.pojo
 * @version:1.0
 */
@Data
@Entity
@Table(name = "tb_student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "s_id")
    private Long sId;
    @Column(name = "s_name")
    private String sName;

    /**
     * 多个学生对应一个老师
     * 注解形式配置多对一
     *  1,配置表关系
     *  2,配置外键
     */
    @ManyToOne(targetEntity = Teacher.class)
    @JoinColumn(name = "s_t_id",referencedColumnName = "t_id")
    private Teacher teacher;
}

代码100分package com.lyf.pojo;

import lombok.Data;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Date:2019-04-12
 * @Description:教师和学生是一对多
 */
@Data
@Entity
@Table(name = "tb_teacher")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "t_id")
    private Long tId;
    @Column(name = "t_name")
    private String tName;
    //配置老师和学生一对多
    /**
     * 注解配置多表关系
     *  1,声名关系
     *  2,配置外键,或者中间表
     *  OneToMany配置一对多
     *      targetEntity设置对应的实体类的类型
     *  JoinColumn 配置外键
     *      name:外键的名称,
     *      referencedColumnName参照的主表的主键字段名称
     */
    @OneToMany(targetEntity = Student.class)
    @JoinColumn(name = "s_t_id",referencedColumnName = "t_id")
    private Set<Student> students = new HashSet<>();
}
 
复制代码

springboot启动类(引导类)

复制代码
package com.lyf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Date:2019-04-12
 * @Description:com.lyf
 * @version:1.0
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}
复制代码

启动引导类,查看数据库会发现表生成成功

dao层代码就不上了,继承JpaRepository就行了

 

接下来我们进行保存操作

 

复制代码
package com.lyf;

import com.lyf.dao.StudentDao;
import com.lyf.dao.TeacherDao;
import com.lyf.pojo.Student;
import com.lyf.pojo.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Date:2019-04-12
 * @Description:com.lyf
 * @version:1.0
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class OneToMoreTest {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;
    @Test
    public void addTest(){
        Student student = new Student();
        student.setSName("老篮孩i");
        Teacher teacher = new Teacher();
        teacher.setTName("刘老师");
        //关联学生和老师,添加学生信息时,还需添加外键的值
        student.setTeacher(teacher);


        studentDao.save(student);
        teacherDao.save(teacher);
    }
}
复制代码

 

结果报错了,发现我是先保存的学生信息,再保存的老师信息,此时数据库中并没有老师的信息,给学生关联老师信息肯定是有问题的

报错信息

 

复制代码
org.springframework.dao.InvalidDataAccessApiUsageException: 
  org.hibernate.TransientPropertyValueException:
     object references an unsaved transient instance - save the transient instance before flushing : com.lyf.pojo.Student.teacher -> com.lyf.pojo.Teacher;
       nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException:
         object references an unsaved transient instance - save the transient instance before flushing : 
          com.lyf.pojo.Student.teacher -> com.lyf.pojo.Teacher
复制代码

 学生表记录插入了,老师表是空的

(转载)Jpa配置一对多关系

 

 

 

改成

 

复制代码
package com.lyf;

import com.lyf.dao.StudentDao;
import com.lyf.dao.TeacherDao;
import com.lyf.pojo.Student;
import com.lyf.pojo.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Date:2019-04-12
 * @Description:com.lyf
 * @version:1.0
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class OneToMoreTest {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;
    @Test
    public void addTest(){
        Student student = new Student();
        student.setSName("老篮孩i");
        Teacher teacher = new Teacher();
        teacher.setTName("刘老师");
        //关联学生和老师,添加学生信息时,还需添加外键的值
        student.setTeacher(teacher);

        //要先保存主表信息
        teacherDao.save(teacher);
        studentDao.save(student);
    }
}
复制代码

 控制台信息,很显然成功了

复制代码
Hibernate: alter table tb_student add constraint FKsojy7bicq68v21slcq9mtwtou foreign key (s_t_id) references tb_teacher (t_id)
2019-04-12 23:29:42.036  INFO 10980 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit "default"
2019-04-12 23:29:42.748  INFO 10980 --- [           main] com.lyf.OneToMoreTest                    : Started OneToMoreTest in 7.77 seconds (JVM running for 9.806)
Hibernate: insert into tb_teacher (t_name) values (?)
Hibernate: insert into tb_student (s_name, s_t_id) values (?, ?)
复制代码

查看数据库也没有问题

(转载)Jpa配置一对多关系

同样我们通过Teacher表也能完成关联操作,保存也是没有问题的

复制代码
@Test
    public void addTest1(){
        Student student = new Student();
        student.setSName("老篮孩i1");
        Teacher teacher = new Teacher();
        teacher.setTName("刘老师1");

        //通过主表来添加关联
        teacher.getStudents().add(student);
        studentDao.save(student);
        teacherDao.save(teacher);
    }
复制代码

 控制打印信息

Hibernate: insert into tb_student (s_name, s_t_id) values (?, ?)
Hibernate: insert into tb_teacher (t_name) values (?)
Hibernate: update tb_student set s_t_id=? where s_id=?

 

学生类和老师类都添加类外键的配置,都具备外键的维护,那么我们这里可以通过学生找到老师,也能通过老师找到学生,这是一种双向关系

如果只配置一方,那就是单向的关系,只能通过指定的一方找到另一方

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8561.html

(0)
上一篇 2023-02-26 12:00
下一篇 2023-02-26

相关推荐

  • Python访问MySQL[亲测有效]

    Python访问MySQL[亲测有效]Python访问MySQL的步骤 创建connection连接,连接数据库 获取cursor游标对象 执行SQL语句 关闭cursor游标对象 关闭connection连接 import pymys…

    2023-03-24
    112
  • Python实现start-process参数传递

    Python实现start-process参数传递
    start-process是Linux操作系统中一个用于启动新进程的命令,它可以通过命令行传递参数和选项,让新进程在创建后可以使用这些参数和选项来执行特定的操作。在Python中,我们也可以使用subprocess模块来实现start-process的功能,传递参数和选项等信息。Python实现start-process参数传递的过程中,需要注意的一些问题,下面将逐一阐述。

    2024-02-23
    83
  • Mysql存储结构「建议收藏」

    Mysql存储结构「建议收藏」索引是一种加快查询速度的数据结构,常用索引结构有hash、B Tree和B+Tree。本节通过分析三者的数据结构来说明为啥Mysql选择用B+Tree数据结构。 数据结构 Hash hash是基于哈希

    2023-02-12
    106
  • 03.事务隔离「建议收藏」

    03.事务隔离「建议收藏」简单来说,事务就是要保证一级数据库操作,要么全部成功,要么全部失败。在MySQL中,事务支持是在引擎层实现的,但MySQL中并不是所有引擎都支持事务,比如Mysql原生的MyISAM引擎就不支持事务…

    2022-12-25
    90
  • Python len函数:返回对象长度

    Python len函数:返回对象长度Python中的blen()/b函数是一个常用的函数,它的作用是返回一个对象的长度或者元素个数,比如字符串、列表、元组、集合、字典等。

    2024-02-24
    83
  • 连接 sql

    连接 sqljava连接sqlserver Spring中连接sqlserver C 连接sqlserver QT连接数据库

    2022-12-17
    92
  • Python Setup.py:打包、安装、分发你的Python应用的便捷工具

    Python Setup.py:打包、安装、分发你的Python应用的便捷工具对于Python开发者来说,Python Setup.py是一个非常重要的工具。通过使用这个工具,你可以将你的Python应用打包成一个安装包并方便地安装到其他机器上去。同时,Python Setup.py还提供了非常好的模板和文档支持,使得开发者可以很轻松地创建自己的Python应用。

    2024-01-10
    54
  • 学会这 2 点,轻松看懂 MySQL 慢查询日志[通俗易懂]

    学会这 2 点,轻松看懂 MySQL 慢查询日志[通俗易懂]MySQL中的日志包括:错误日志、二进制日志、通用查询日志、慢查询日志等等。这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志。 1)通用查询日志:记录建立的客户端连接和执行的语句。 2)慢…

    2023-03-18
    103

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注