S
S
s2019-04-30 23:19:03
MySQL
s, 2019-04-30 23:19:03

Why are links not established during table auto-generation?

Why, after auto-generation of tables,
spring.jpa.hibernate.ddl-auto=create
there are no links between tables in the database using hibernate? And how can they be installed?
This is how the database tables look like:
5cc8acdca8a34207277122.png
In fact, two classes are inherited from the user in the code: student, teacher.
the student has a many-to-many relationship to the room; the teacher has a one-to-many relationship.
Listing of models/entities:

@Entity
@Table(name = "rooms")
public class Room {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "name")
    String name;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "teacher_id")
    private Teacher teacher;
    @ManyToMany(mappedBy = "createdRooms")
    private List<Student> students;
}

@Entity
@Table(name = "users")
@Inheritance
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "login")
    private String login;

    @Column(name = "password")
    private String password;
}

@Entity
@DiscriminatorValue("TEACHER")
public class Teacher extends User {
    @OneToMany(mappedBy = "teacher",
            cascade = CascadeType.ALL,
            orphanRemoval = true)
    private List<Room> createdRooms;
}

@Entity
@DiscriminatorValue("STUDENT")
public class Student extends User {
    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE})
    @JoinTable(name = "students_rooms",
            joinColumns = @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "room_id"))
    private List<Room> createdRooms;
}

What could be the reason? And how to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question