java 我有“User”类和2个继承自它的类“Student”和“Teacher”,但是当我运行spring Boot 应用程序时,它只创建“User”表

o0lyfsai  于 2023-05-21  发布在  Java
关注(0)|答案(2)|浏览(152)

我正在使用jakarta,当我运行应用程序时,没有创建“学生”和“教师”表,只创建了“用户”表。会有什么问题呢?

用户类

import jakarta.persistence.*;

import java.io.Serializable;

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String first_name;
    private String last_name;

    public User(String first_name, String last_name) {
        this.first_name = first_name;
        this.last_name = last_name;
    }

    public User() {}
}

学生类

import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;

import java.io.Serializable;
import java.util.Set;

@Entity
public class Student extends User implements Serializable {

    @Id
    @GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;

    private String first_name;
    private String last_name;

    @JsonManagedReference
    @ManyToMany(mappedBy = "students")
@JoinTable(
        name = "student_module",
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "module_id"))
private Set<Module> modules;

    private double moyenneGenerale;

    public Student(String first_name, String last_name) {
        super(first_name, last_name);
    }

    public Student() {
    }
}

模块类:

import jakarta.persistence.*;

import java.io.Serializable;
import java.util.Set;

@Entity
public class Module implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;

    @OneToMany
    private Set<Subject> subjects;

    @ManyToMany(mappedBy = "modules")
    private Set<Student> students;

    public Module(Set<Subject> subjects) {

    }

    public Module() {}

    public Long getId() {
        return id;
    }

    public Set<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(Set<Subject> subjects) {
        this.subjects = subjects;
    }

    public void calculMoyenne() {

    }
}

主题类:

import jakarta.persistence.*;

@Entity
public class Subject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String name;
    private int coefficient;

    @ManyToOne
    @JoinColumn(name = "module_id")
    private Module module;

    public Subject(String name, int coefficient, Module module) {
        this.name = name;
        this.coefficient = coefficient;
        this.module = module;
    }

    public Subject() {}

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCoefficient() {
        return coefficient;
    }

    public void setCoefficient(int coefficient) {
        this.coefficient = coefficient;
    }

}

错误7576 --- [ main] j.LocalContainerEntityManagerFactoryBean:无法初始化JPA EntityManagerFactory:集合“com.example.grademanagement_hamzarami.models.Student.modules”是名为“student”的属性“mappedBy”,该属性在目标实体“com.example.grademanagement_hamzarami.models.Module”中不存在
创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:集合“com.example.grademanagement_hamzarami.models.Student.modules”是名为“student”的属性“mappedBy”,该属性在目标实体“com.example.grademanagement_hamzarami.models.Module”中不存在

wydwbb8l

wydwbb8l1#

您的Module @Entity缺少student字段,因此您的模块表缺少此列。
但是,我怀疑你有一个模型问题。
Student和Module之间的关系是ManyToMany(即关联),即一个Student可以在许多Module中扮演角色,并且一个Module可以呈现给许多Student。
This几乎相同的模型,如果你替代模块课程.

cwxwcias

cwxwcias2#

修改学生实体,如下所示:

@JsonManagedReference
@ManyToMany(mappedBy = "students")
private List<Module> modules;

而且你应该使用Set而不是List来处理所有的关系。
EDIT:每个实体都必须为每个字段设置getter和setter。

相关问题