Hibernate: There is already a class, there are already many students, establish the relationship between many students and the class.

  mysql, question

Hibernate problem
There is already a class, there are already many students, establish the relationship between many students and the class.
How to realize this, please consult your great gods
This is a class that already exists and a student that already exists. Establish the relationship between the class and the student.
@Test

public void testR_1() {
 Session session = sessionFactory.openSession();
 Transaction transaction = session.beginTransaction();
 Classes classes1 = (Classes) session.get(Classes.class, 1L);
 Student student = (Student) session.get(Student.class, 6L);
 classes1.getStudents().add(student);
 transaction.commit();
 session.close();
 bracket

public class Classes implements Serializable {

private Long cid;
 private String cname;
 private String description;
 private Set<Student> students;
 
 public Long getCid() {
 return cid;
 bracket
 
 public void setCid(Long cid) {
 this.cid = cid;
 bracket
 
 public String getCname() {
 return cname;
 bracket
 
 public void setCname(String cname) {
 this.cname = cname;
 bracket
 
 public String getDescription() {
 return description;
 bracket
 
 public void setDescription(String description) {
 this.description = description;
 bracket
 
 public Set<Student> getStudents() {
 return students;
 bracket
 
 public void setStudents(Set<Student> students) {
 this.students = students;
 bracket

bracket

Xml configuration
 <?  xml version="1.0" encoding="utf-8"?  >
 <!  DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
 <class name="com.liuyang.domain.Classes">
 <id name="cid" length="5" type="java.lang.Long">
 <generator class="increment"></generator>
 </id>
 <property name="cname" length="20" type="java.lang.String"></property>
 
 <property name="description" length="100" type="java.lang.String"></property>
 <!  --
 The set element corresponds to the set set set in the class
 Associating classes Table with student Table through set Element
 Key is to associate two tables in the form of foreign keys.
 One-to-many associates two classes in the form of classes.
 
 Cascade cascade
 save-update
 1, when saving the class, how to operate on the students
 If the student object does not have a corresponding value in the database, the save operation will be performed at this time.
 If the student object has a corresponding value in the database, update will be performed at this time.
 Delete
 All
 Inverse maintains relationships
 True does not maintain relationships
 False maintain relationship
 default   false
 -->
 <set name="students" cascade="all">
 <!  --
 Key is used to describe foreign keys.
 -->
 <key>
 <column name="cid"></column>
 </key>
 <one-to-many class="com.liuyang.domain.Student"/>
 </set>
 </class>
 </hibernate-mapping>

This is what I did

11, there is already a class, there are already many students, establish the relationship between many students and the class
 */
 @Test
 public void testR_2() {
 Session session = sessionFactory.openSession();
 Transaction transaction = session.beginTransaction();
 Classes classes = (Classes) session.get(Classes.class, 2L);
 Set<Student> students = classes.getStudents();
 List<Student> sList = new ArrayList<Student>(students);
 for (int i = 0;   i < sList.size();  I plus)
 if (sList.get(i).getSid().longValue() == 3
 || sList.get(i).getSid().longValue() == 5) {
 sList.remove(sList.get(i));
 i--;
 bracket
 bracket
 students = new HashSet<Student>(sList);
 classes.setStudents(students);
 transaction.commit();
 session.close();
 bracket

However, it can only be found and the update cannot be loaded. Please ask the great gods how to handle it. Thank you. Thank you~~

I have 100 dollars and a good idea. How do I start a business?