package com.xj.model;import java.util.HashSet;
import java.util.Set;import javax.persistence.*;import org.hibernate.annotations.BatchSize;@Entity
@BatchSize(size=5)
public class Student {
private int id;
private String  name;
private Set<Course> courses=new HashSet<Course>();
@ManyToMany
@JoinTable(
name="score",
joinColumns=@JoinColumn(name="student_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="course_id", referencedColumnName="id")
        )
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
@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;
}}
package com.xj.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;import org.hibernate.annotations.BatchSize;@Entity
@BatchSize(size=5)
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;
}}
package com.xj.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;import org.hibernate.annotations.BatchSize;@Entity
@BatchSize(size=5)
@Table(name="score")
public class Score {
private int id;
private int s;
private Course c;
private Student t;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="course_id")
public Course getC() {
return c;
}
public void setC(Course c) {
this.c = c;
}
@ManyToOne
@JoinColumn(name="student_id")
public Student getT() {
return t;
}
public void setT(Student t) {
this.t = t;
}
public int getS() {
return s;
}
public void setS(int s) {
this.s = s;
}}
@Test
public void testSaveSCT() {
Student s1=new Student();
s1.setName("xj");
Student s2=new Student();
s2.setName("hs");
Course c1=new Course();
c1.setName("java");
Course c2=new Course();
c2.setName("english");

/*s1.getCourses().add(c1);
s1.getCourses().add(c2);
s2.getCourses().add(c1);
s2.getCourses().add(c2);*/

Score sc1=new Score();
sc1.setT(s1);
sc1.setC(c1);
sc1.setS(90);

Score sc2=new Score();
sc2.setT(s1);
sc2.setC(c2);
sc2.setS(95);

Score sc3=new Score();
sc3.setT(s2);
sc3.setC(c1);
sc3.setS(70);

Score sc4=new Score();
sc4.setT(s2);
sc4.setC(c2);
sc4.setS(60);

Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s1);
session.save(s2);
session.save(c1);
session.save(c2);
session.save(sc1);
session.save(sc2);
session.save(sc3);
session.save(sc4);

session.getTransaction().commit();


}
简单点说就是student与course表之间以score表为中间表做映射 可是做save操作时报那样的错误 怎么调都调不好 求高手赐教赐教 非常感谢啊

解决方案 »

  1.   

    很好··· 我昨天就是报的这个错误·· 当时找了很久才找到解决方案
    我相信你也在baidu上面查了一些相关的资料。
    遇到这种错误时有几种解决方案,最常见的就是,你的表之间的关系映射有可能出错,还有一种就是你在新增的时候没有考虑到表与表之间的关系。
      

  2.   

    /*s1.getCourses().add(c1);
    s1.getCourses().add(c2);
    s2.getCourses().add(c1);
    s2.getCourses().add(c2);*/
    我就是不知道加了注释的这段应不应该去掉 去了不行 不去了还是一样的不行 我觉得student与course之间已经通过互相与score建立了映射而或得了关系 所以我不知道它们两之间的多对多的关系映射在插入值的时候还需不需要显示的建立
      

  3.   

    Could not execute JDBC batch update ,不能进行数据更新
    这应该关系到那个表的主码设置的有问题吧,要么你得映射出现了问题
    在bhm.xml中的集合段中你设置了级联选项了没?
      

  4.   

    在bhm.xml中的集合段中你设置了级联选项,例如:
    <set name="" column="" fetch="join"></set>
      

  5.   


    我使用的是Annotation  不是xml