插入:
在Student中添属性SC(SC表示课程表);
Student student = new Student();
SC sc = new SC();
student.setSC(sc);
在hibernate配置文件中设置cascade=true,getHibernateTemplate.save(student);
更新与插入类试;
getHibernateTemplate.update(student);
在Student中添属性SC(SC表示课程表);
Student student = new Student();
SC sc = new SC();
student.setSC(sc);
在hibernate配置文件中设置cascade=true,getHibernateTemplate.save(student);
更新与插入类试;
getHibernateTemplate.update(student);
<hibernate-mapping>
<class name="com.ssh.Student" table="student" schema="dbo" catalog="sshcourse">
<id name="stuId" type="integer">
<column name="stu_id" />
<generator class="native" />
</id>
<property name="stuName" type="string">
<column name="stu_name" length="50" />
</property>
<property name="stuPwd" type="string">
<column name="stu_pwd" length="50" />
</property>
<set name="courses"
table="course_student_table"
cascade="save-update"
inverse="false"
lazy="false">
<key column="stu_id"></key>
<many-to-many class="com.ssh.Course" column="cou_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
课程Course.hbm.xml:
<hibernate-mapping>
<class name="com.ssh.Course" table="course" schema="dbo" catalog="sshcourse">
<id name="couId" type="integer">
<column name="cou_id" />
<generator class="native" />
</id>
<property name="couName" type="string">
<column name="cou_name" length="50" />
</property>
<property name="couDis" type="string">
<column name="cou_dis" length="50" />
</property>
<set name="students"
table="course_student_table"
cascade="save-update"
inverse="true">
<key column="cou_id"></key>
<many-to-many class="com.ssh.Student" column="stu_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
他们之间是通过course_student_table学生课程表关联的
table="course_student_table"
cascade="save-update"
inverse="false"
lazy="false">
<key column="stu_id"></key>
<many-to-many class="com.ssh.Course" column="cou_id"></many-to-many>
</set>
<set name="students"
table="course_student_table"
cascade="save-update"
inverse="true">
<key column="cou_id"></key>
<many-to-many class="com.ssh.Student" column="stu_id"></many-to-many>
</set>上面的inverse="false" lazy="false"和下面的inverse="true"有什么区别?
能否给出增加、修改、删除的方法?
比如增加一个学生,同时增加这个学生的选课信息
修改一个学生,同时修改这个学生的选课信息
删除一个学生,同时删除这个学生的选课信息
给出简单的方法就可以了,谢谢
窝做了一个删除你看看。
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
CourseID courseid=(CourseID)form;
Student student=studao.findById((Integer)(request.getSession().getAttribute("stuid")));
if(courseid.getCourses().length>0)
{
for(int i=0;i<courseid.getCourses().length;i++)
{
Integer cid=new Integer(courseid.getCourses()[i]);
Course course=coudao.findById(cid);
student.getCourses().remove(course);
System.out.println("this is test1 sdfsdfg");
System.out.println(cid);
}
}
else
{
System.out.println("null");
}
studao.delete(student);
return mapping.findForward("success");
}
我做的是当vv用户登录时,它选择一一科,那在数据库中就把学生表的vv用户删除,同时把vv用户的选课信息也删除了。道理差不多,你改改。