hbm.xml:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.po"> <!-- 学生信息 -->
<class name="Student" table="student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property> <set name="courses" table="table_stu_course" cascade="all-delete-orphan"
lazy="false">
<key column="s_id"></key>
<many-to-many class="Course" column="c_id" />
</set>
<many-to-one name="teacher" column="t_id" class="Teacher"
not-found="ignore" lazy="false" />
</class> <!-- 教师信息 -->
<class name="Teacher" table="teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="stuSet"   cascade="all-delete-orphan" >
<key column="id"></key>
<one-to-many class="Student" />
</set>

<set name="courseSet"  table="teaching_teacher_course" cascade="all-delete-orphan" >
<key column="t_id"></key>
<many-to-many class="Course" column="c_id"/>
</set>
</class>
<!-- 课程信息 -->
<class name="Course" table="course">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="students" table="table_stu_course" cascade="all-delete-orphan">
<key column="c_id"></key>
<many-to-many class="Student" column="s_id" />
</set> <set name="teachers" table="teaching_teacher_course" cascade="all-delete-orphan">
<key column="c_id"></key>
<many-to-many class="Teacher" column="t_id" />
</set>
</class></hibernate-mapping>测试类:
testMain:package struts.action;import hibernate.po.Course;
import hibernate.po.Student;
import hibernate.po.Teacher;import java.util.HashSet;
import java.util.List;
import java.util.Set;import org.hibernate.Session;
import org.hibernate.Transaction;import util.HibernateSessionFactory;public class testMain {

public void test()
{
Transaction  tx = null ;
List  list = null;
Session session = HibernateSessionFactory.getSession();
 
tx = session.beginTransaction();

Teacher teacher = new Teacher();
teacher.setName("鸿");

Course   course = new Course(); //从老师到课程
course.setName("英语");
Course   course_ = new Course();
course_.setName("数学");
Set<Course> courseSet = new HashSet<Course>();
courseSet.add(course);
courseSet.add(course_);
teacher.setCourseSet(courseSet);

Student  stu = new Student();  //从老师到学生,从学生到课程
stu.setName("王");
stu.setCourses(courseSet);
Student  stu_ = new Student();
stu_.setName("里");
stu_.setCourses(courseSet);
Set<Student> stuSet = new HashSet<Student>();
stuSet.add(stu);
stuSet.add(stu_);
teacher.setStuSet(stuSet);


session.saveOrUpdate(teacher) ;
session.flush();
tx.commit();
        session.close();
 
} public static void main(String[] args)
{
testMain main = new testMain();
main.test();
}
}
报错:010-01-29 14:48:05,140 WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: 1062, SQLState: 23000>
2010-01-29 14:48:05,140 ERROR [org.hibernate.util.JDBCExceptionReporter] - <Duplicate entry '3' for key 1>
2010-01-29 14:48:05,140 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1119)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at struts.action.testMain.test(testMain.java:51)
at struts.action.testMain.main(testMain.java:60)
Caused by: java.sql.BatchUpdateException: Duplicate entry '3' for key 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 13 more
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1119)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at struts.action.testMain.test(testMain.java:51)
at struts.action.testMain.main(testMain.java:60)
Caused by: java.sql.BatchUpdateException: Duplicate entry '3' for key 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 13 more

解决方案 »

  1.   

    “Could not execute JDBC batch update ”
    无法执行的JDBC批量更新
      

  2.   

    Course  course = new Course(); //从老师到课程 
    course.setName("英语"); 
    Course  course_ = new Course(); 
    course_.setName("数学"); Student  stu = new Student();  //从老师到学生,从学生到课程 
    stu.setName("王"); 
    stu.setCourses(courseSet); 
    Student  stu_ = new Student(); 
    stu_.setName("里"); 先把这些对象保存了
      

  3.   


    这样的, 是想实现一个hibernate 级联保存的问题.一共三个类, 5张表.
    老师和学生是一对多,学生和课程之间是多对多, 老师和课程之间是多对多....
      

  4.   

    Duplicate entry '3' for key 1 ?主键重复
      

  5.   

    3个po的定义:
    public class Course { String id ="";
    String name ="";
    Set<Student> students = new HashSet<Student>();
    Set<Teacher> teachers = new HashSet<Teacher>();
      get,set方法...
    }   public class Student {
    String id ="";
    String name ="";
    Teacher teacher ;
    Set <Course> courses=  new HashSet<Course>();
      get,set方法...}
    public class Teacher { String id ="";
    String name ="";
    Set<Student> stuSet = new HashSet<Student>();
    Set<Course> courseSet = new HashSet<Course>();
         get,set方法...}
      

  6.   

    主键自动增长,错误.你分配给它一个ID就差不多了.
    不知道谁作为主角,inverse都是默认值.
    都是新的对象为什么要saveOrUpdate(),直接save()