日期:2014-05-20  浏览次数:20956 次

关于多对多的关联问题,求大神进来看下
数据库用到是Oracle
数据库的设计为三张表,stus,course,score
对应的类stus
@Entity
public class Stus {
private int id;
private String name;
private Set<Course> coures=new HashSet<Course>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="score", joinColumns={@JoinColumn(name="student_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="coure_id", referencedColumnName="id")})

public Set<Course> getCoures() {
return coures;
}
public void setCoures(Set<Course> coures) {
this.coures = coures;
}



}
对应的course
@Entity
public class Course {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}
对应的类score
@Entity
public class Score {
private int id;
private int score;
private Stus student;
private Course course;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@ManyToOne
@JoinColumn(name="student_id")
public Stus getStudent() {
return student;
}
public void setStudent(Stus student) {
this.student = student;
}
@ManyToOne
@JoinColumn(name="course_id")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}


}
测试类

@Test
public void testScore(){
Stus student=new Stus();
Course course=new Course();
Score score=new Score();
student.setName("july");
course.setName("java");
score.setScore(90);
score.setStudent(student);
score.setCourse(course);
Session s=sf.getCurrentSession();
s.beginTransaction();
s.save(student);
s.save(course);
s.save(score);
s.getTransaction().commit();
}
@Test
public void testGetScore(){
testScore();
Session s=sf.getCurrentSession();
s.beginTransaction();
Stus student=(Stus)s.get(Stus.class, 1);
s.getTransaction().commit();
System.out.println(student.getName());
for(Course c:student.getCoures()){
System.out.println(c.getName());
}
}
在做查询的时候,fetch不设的时候,从suts表查询没有问题,把fetch设为eager的时候,连带查询course的时候报错
错误如下:
ibernate: insert into Stus (name, id) values (?, ?)
Hibernate: insert into Course (name, id) values (?, ?)
Hibernate: insert into Score (course_id, score, student_id, id) values (?, ?, ?, ?)
Hibernate: select stus0_.id as id9_1_, stus0_.name as name9_1_, coures1_.student_id as student1_3_, course2_.id as coure2_3_, course2_.id as id10_0_, course2_.name as name10_0_ from Stus stus0_ left outer join score coures1_ on stus0_.id=coures1_.student_id left outer join Course course2_ on coures1_.coure_id=course2_.id where stus0_.id=?
09:57:40,843 WARN JDBCExceptionReporter:100 - SQL Error: 904, SQLState: 42000
09:57:40,859 ERROR JDBCExceptionReporter:101 - ORA-00904: "COURES1_"."COURE_ID": 标识符无效
实在看不明白,求解释啊


------解决方案--------------------