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


#1

It’s a OneToMany Bi-directional mapping. I’m generating the table through
hbm2ddl.auto= 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.

Student.java

@Entity
@Table(name="STUDENT_DETAILS1")
public class Student {
	private int studentId;
	private String name;
	private long mobile;
	private List<Courses> course;
	
	@Id
	@GenericGenerator(name="gen",strategy="increment")
	@GeneratedValue(generator="gen")
	@Column(name="STUDENT_ID")
	@Type(type="int")
	public int getStudentId() {
		return studentId;
	}
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}
	@Column(name="NAME")
	@Type(type="string")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="MOBILE")
	@Type(type = "long")
	public long getMobile() {
		return mobile;
	}
	public void setMobile(long mobile) {
		this.mobile = mobile;
	}
	@OneToMany(targetEntity=Courses.class,mappedBy="student",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	public List<Courses> getCourse() {
		return course;
	}
	public void setCourse(List<Courses> course) {
		this.course = course;
	}

Courses.java

@Entity
@Table(name="COURSE_DETAILS1")
public class Courses {
	private int courseId;
	private String courseName;
	private int duration;
	private Student student;
	
	@Id
	@GenericGenerator(name="gen1",strategy="increment")
	@GeneratedValue(generator="gen1")
	@Column(name="COURSE_ID")
	@Type(type="int")
	public int getCourseId() {
		return courseId;
	}
	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}
	@Column(name="NAME")
	@Type(type="string")
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	@Column(name="DURATION")
	@Type(type="int")
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	@ManyToOne(targetEntity=Student.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="ID",referencedColumnName="STUDENT_ID")
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

}

StackTrace

Hibernate: 
    
    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.


#2

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?

Thanks


#3

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.


#4

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


#5

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.


#6

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