ManyToOne- Alter Table Query is generating every time when inserting a value

It’s a OneToMany Bi-directional mapping. I’m generating the table through update automatically.
It’s working fine but the problem in first execution it’s generating the tables and altering it and adding FK constarints.
But after first execution for each new insertion it’s generating a alter table query first. But it’s supposed to do it only in first execution not every time.

public class Student {
	private int studentId;
	private String name;
	private long mobile;
	private List<Courses> course;
	public int getStudentId() {
		return studentId;
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	@Type(type = "long")
	public long getMobile() {
		return mobile;
	public void setMobile(long mobile) { = mobile;
	public List<Courses> getCourse() {
		return course;
	public void setCourse(List<Courses> course) {
		this.course = course;

public class Courses {
	private int courseId;
	private String courseName;
	private int duration;
	private Student student;
	public int getCourseId() {
		return courseId;
	public void setCourseId(int courseId) {
		this.courseId = courseId;
	public String getCourseName() {
		return courseName;
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	public int getDuration() {
		return duration;
	public void setDuration(int duration) {
		this.duration = duration;
	public Student getStudent() {
		return student;
	public void setStudent(Student student) {
		this.student = student;



    alter table COURSE_DETAILS1 
       add constraint FKtjj24cldtk15avmc9myshuse8 
       foreign key (ID) 
       references STUDENT_DETAILS1 (STUDENT_ID)

The above query is generating every time whenever i perform any insertion operation.
Is there anything I’ve missed.

Hi @Stone,

can you tell me which version of Hibernate are you using? and what database?

Anyway you mean the alter table statement is executed every time you re-create the SessionFactory, don’t you?


That cannot be true. An entity INSERT has nothing to do with the DDL generation which happen during bootstrap.

To prove it, try to write a replicating test case that proves your issue and send it to us.

yes and sorry for the misinterpretation you are right it generates the alter query whenever I bootstrap the hibernate. For every individual first run it generating the alter query but it should do once only in the first run.
Hibernate Version- 5.2.17.Final

sorry for my wrong interpretation not while insert operation, it will generating the alter query whenever I’m running the application(bootstrapping the hibernate) but why for every run it’s altering the table. It has supposed to be done only while creating the table dynamically i.e only in the first run not every time.

Try to replicate it with this test case and open a Jira issue once you have the test to prove it.