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.