我刚刚开始学习Hibernate,我只是通过书中的示例,并且有一些奇怪的与ManyToMany关系的例子,这里是代码:
关系的左侧
package com.examscm.mappings;
import com.examscm.HibernateUtil;
import org.hibernate.Session;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by me on 20.06.15.
*/
@Entity
@Table(schema = "HibernateBook")
public class LeftManyStudents {
private long id;
private String studentName;
List<RightManyCourses> courses = new ArrayList<>();
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@ManyToMany(targetEntity = RightManyCourses.class, cascade = CascadeType.ALL)
@JoinTable(name = "join_table", schema = "HibernateBook",
joinColumns = { @JoinColumn(name = "lmstudent_id")},
inverseJoinColumns = {@JoinColumn(name = "rmcourse_id")})
public List<RightManyCourses> getCourses(){
return courses;
}
public void setCourses(List<RightManyCourses> courses){
this.courses = courses;
}
public static void main(String[] args){
HibernateUtil.recreateDatabase();
/*Creating and saving instances of left and right side
side of relationship*/
}
}
关系的右侧:
package com.examscm.mappings;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by me on 20.06.15.
*/
@Entity
@Table(schema = "HibernateBook")
public class RightManyCourses {
private long id;
private String courseCode;
List<LeftManyStudents> students = new ArrayList<>();
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
@ManyToMany(targetEntity = LeftManyStudents.class,
cascade = CascadeType.ALL)
@JoinTable(name = "join_table", schema = "HibernateBook",
joinColumns = { @JoinColumn(name = "rmcourse_id")},
inverseJoinColumns = {@JoinColumn(name = "lmstudent_id")}
)
public List<LeftManyStudents> getStudents() {
return students;
}
public void setStudents(List<LeftManyStudents> students){
this.students = students;
}
}
重新创建数据库代码:
public static void recreateDatabase(){
Configuration configuration = getInitializedConfiguration();
new SchemaExport(configuration).create(true, true);
}
问题是,当存在默认的Class to Column name映射时,everythink工作正常,但当我更改它时,例如:
@Table(name = "left_many_students", schema = "HibernateBook")
@Table(name = "right_many_courses", schema = "HibernateBook")
我收到了以下错误
cze 20, 2015 10:21:59 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Table 'HibernateBook.join_table' doesn't exist
所以我手动创建它然后下一个错误:
cze 20, 2015 10:23:50 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table HibernateBook.join_table drop foreign key FK_22m6gyyey9mu21mlfbge6uguj
cze 20, 2015 10:23:50 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Can't DROP 'FK_22m6gyyey9mu21mlfbge6uguj'; check that column/key exists
cze 20, 2015 10:23:50 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table HibernateBook.player drop foreign key FK_5q11flfd61t4n9eepixi8ltup
cze 20, 2015 10:23:50 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Can't DROP 'FK_5q11flfd61t4n9eepixi8ltup'; check that column/key exists
但这真的很奇怪,因为如果我只是从这两个段落中移除@Entity并重新创建数据库,那么每个方面都可以正常工作,没有外键错误。
我不知道问题是什么,有人能告诉我吗?