skip to Main Content

I’am using jakarta and when I run the app the "Student" and "Teacher" tables didn’t created and only the "User" table is created. What could be the problem ?

User class

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() {}
}

Student class

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")
    private Set<Module> modules;

    private double moyenneGenerale;


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

    public Student() {
    }
}

Module class:

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
    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() {

    }
}

Subject class:

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

}

ERROR 7576 — [ main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA
EntityManagerFactory: Collection
‘com.example.grademanagement_hamzarami.models.Student.modules’ is
‘mappedBy’ a property named ‘student’ which does not exist in the
target entity ‘com.example.grademanagement_hamzarami.models.Module’

Error creating bean with name ‘entityManagerFactory’ defined in class
path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Collection
‘com.example.grademanagement_hamzarami.models.Student.modules’ is
‘mappedBy’ a property named ‘student’ which does not exist in the
target entity ‘com.example.grademanagement_hamzarami.models.Module’

2

Answers


  1. Your Module @Entity lacks a student field so your module table lacks this column.

    However, I suspect that your have a model problem.

    The relationship between Student and Module is ManyToMany (aka associative), ie a Student can enrole in many Modules and a Module can be presented to many Students.

    This almost the same model if you substitute Course of Module.

    Login or Signup to reply.
  2. Modify Student entity like this:

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

    And also you should use Set instead of List for all the relations.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search