'How can I let Hibernate auto-create tables in my database?

here is the background: I am using IntelliJ 2021.3 for Spring development. My current task is to let an embedded H2 database get initialized with a table schema and test data.

My current application should basically support that behaviour. But what am I missing?

Now I did my research googling through many articles that describe the auto-generation of tables in a database with hibernate mostly in the same way: I have to set spring.jpa.hibernate.ddl-auto in the aplication.properties to create.

spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create

I have set up my Entities like this here:

package com.learning.classes;

import javax.persistence.*;
import java.util.List;

@Entity
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Course_Id")
    private long courseId;
    @Column(name = "Course_Name")
    private String courseName;

    @OneToMany(mappedBy = "course", cascade = CascadeType.PERSIST)
    private List<Student> student;

    public Course() {
    }

    public Course(long courseId, String courseName) {
        this.courseId = courseId;
        this.courseName = courseName;
    }

    public long getCourseId() {
        return courseId;
    }

    public void setCourseId(long courseId) {
        this.courseId = courseId;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public List<Student> getStudent() {
        return student;
    }

    public void setStudent(List<Student> student) {
        this.student = student;
    }
}

and

package com.learning.classes;

import javax.persistence.*;

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Student_Id")
    private long studentId;
    @Column(name = "First_Name")
    private String firstName;
    @Column(name = "Last_Name")
    private String lastName;

    @ManyToOne(fetch = FetchType.EAGER)
    private Course course;

    public Student() {
    }

    public Student(long studentId, String firstName, String lastName) {
        this.studentId = studentId;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public long getStudentId() {
        return studentId;
    }

    public void setStudentId(long studentId) {
        this.studentId = studentId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }
}

Now I know that there ain't any data to get inserted when starting the app, but at least I should see my tables when I access my H2 via the console.

Thanks in advance.



Solution 1:[1]

So I found out why nothing is working, and to be fully open about it: it was my fault.

I arranged the packages in a way where the Main was not able to read the other packages. The main package was not in the same directory as the others so nothing could happen.

I am writing this so anyone can account on that.

Nevertheless thanks for everyone who has contributed to this topic.

This question is closed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Toni