java—子表jpa的持久化问题

ruoxqz4g  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(404)

我在mysql中有一个onetomany关系,我用jpaMap了它。当从数据库中检索信息时,它工作得很好,但是在持久化子表时,我遇到了一个问题。erd图片
--mysql工作台正向工程

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`course` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `descr` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `mydb`.`topic`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`topic` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `descr` VARCHAR(45) NULL,
  `course_id` INT NOT NULL,
  PRIMARY KEY (`id`, `course_id`),
  INDEX `fk_topic_course_idx` (`course_id` ASC) VISIBLE,
  CONSTRAINT `fk_topic_course`
    FOREIGN KEY (`course_id`)
    REFERENCES `mydb`.`course` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

@Entity
@Table(name = "course", schema = "mydb")
public class Course implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1886462862094528507L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "descr")
    private String descr;

    @OneToMany(mappedBy = "course", fetch = FetchType.EAGER) //name of object in Topic entity  
    @JsonManagedReference
        private Set<Topic> topic; 
}

@Entity
@Table(name="topic", schema = "mydb")
public class Topic implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -365302645107748753L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false)
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "descr")
    private String descr;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="course_id") //name of the foreign key. Must pay attention here 
    @JsonBackReference
    private Course course;
}

我试图通过使用以下json发出post请求来持久化子表 course_id “这是 foreign key ,但我得到一个错误。

{
    "id": 1,
    "name: "Java",
    "descr": "This topic goes over Java core",
    "course_id": 1
 }

2019-11-14 13:11:27.013错误18916---[nio-8081-exec-2]o.a.c.c.c.[/].[dispatcherservlet]:路径为[]的上下文中servlet[dispatcherservlet]的servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.dao.dataintegrityviolationexception:无法执行语句;sql[不适用];约束[null];嵌套异常为org.hibernate.exception.constraintviolationexception:无法执行具有根本原因的语句]
java.sql.sqlintegrityconstraintviolationexception:列'course\u id'不能为空

d4so4syb

d4so4syb1#

发生错误的原因是您正在使用 @GeneratedValue 无附加规范。默认情况下,hibernate将尝试访问名为 <schema-name>.hibernate-sequence .
因为您使用的是mysql,所以可以注解:

@GeneratedValue(strategy = GenerationType.IDENTITY)

这将导致使用mysql的 AUTO_INCREMENT 而不是顺序。
您还需要使用 AUTO_INCREMENT 创建架构时。
您还缺少对的引用 Course 持久化前的实体:

topic.setCourse(entityManager.getReference(Course.class, courseId));
``` `courseId` 这是你的名字 `course_id` 作为json传递。

相关问题