我是Hibernate的新手。我试图从数据库中读取对象并进行更新 考虑这种双向映射
STUDENT ---> COURSES
One Many
的hbm.xml的:
Student.hbm.xml:
<set name="courses" table="COURSES" inverse="false" >
<key>
<column name="STUDENT_ID" />
</key>
<one-to-many class="Courses" />
</set>
Courses.hbm.xml:
<many-to-one name="student" class="Student" >
<column name="STUDENT_ID" />
</many-to-one>
Java代码:
Session session = Utility.getSessionFactory().openSession();
session.beginTransaction();
Student student = (Student)session.get(Student.class, 1);
Courses course = new Courses();
course.setPriceOpen(new Float("1.2"));
course.setPriceClose(new Float("1.1"));
course.setPrceChange(new Float("10.0"));
course.setDate(new Date());
course.setStudent(student);
student.getCourses().add(course);
session.save(course);
session.update(student);
session.getTransaction().commit();
session.close();
从数据库加载 学生 对象后,我尝试将 新课程 添加到< strong> 加载学生实例 但是,我确实得到了这个错误
Exception in thread "main" org.hibernate.HibernateException:
identifier of an instance of Course was altered from 1 to 0.
有关如何更新该Student实例的任何建议,以便此学生实例现在将分配2个课程。