an unresolved thought

This is a javabean class with a one-to-many relationship. Note that there is a School parameter in the Student class. Normally, when we write javabeans, we only write a set collection on the School side. Student writes a School, it is really possible to directly find a student's school?

Does that mean that if we write normal javabeans, we can only add data to the foreign key in the database, and cannot realize the realization of finding a student's school?

So if hibernate is written like this, is it not possible to achieve it?

Secondly, why can we add foreign keys to the student table in the database when we use school.getAllStudent().add(new student());?

1  import java.util.* ;
 2  public  class TestDemo{ // Set the relationship between school and students 
3      public  static  void main(String args[]){
 4          School sch = new School("Tsinghua University") ; // Instantiate School object 
5          Student s1 = new Student("Zhang San",21) ; // Instantiate student object 
6          Student s2 = new Student("Li Si",22 ) ;
 7          Student s3 = new Student("Wang Wu", 23 ) ;
 8          sch.getAllStudents().add(s1) ;// Add student 
9 to school          sch.getAllStudents().add(s2) ;
 10          sch.getAllStudents().add(s3) ;
 11          s1.setSchool(sch) ; // A student belongs to a school   
12          s2.setSchool (sch) ;
 13          s3.setSchool(sch) ;
 14          System.out.println(sch) ;
 15          Iterator<Student> iter = sch.getAllStudents().iterator() ;
 16          while (iter.hasNext()){
 17              System.out.println("\t|- " + iter.next()) ;
 18          }
 19      }
 20  }
 21  
22  //Student class 
23  class Student{
 24      private String name ;
 25      private  int age ;
 26      private School school; // A student belongs to a school 
27      public Student(String name, int age){
 28          this .setName(name) ;
 29          this . setAge(age) ;
 30      }
 31      public  void setSchool(School school){
 32          this .school = school ;
 33      }
 34      public School getSchool(){
 35         return this.school ;
36     }
37     public void setName(String name){
38         this.name = name ;
39     }
40     public void setAge(int age){
41         this.age = age ;
42     }
43     public String getName(){
44         return this.name; 
45     }
46     public int getAge(){
47         return this.age ;
48     }
49      public String toString(){
 50          return "Student name: " + this .name + "; age: " + this .age ;
 51      }
 52  }
 53  
54  // School class 
55  class School{
 56      private String name ;
 57      private List<Student> allStudents ; // One school has multiple students   
58      public School(String name){
 59          this .allStudents = new ArrayList<Student> (); 
 60          this .setName(name) ;
 61      }
62      public  void setName(String name){
 63          this .name = name ;
 64      }
 65      public String getName(){
 66          return  this .name; 
 67      }
 68      public List<Student> getAllStudents(){ // Get all students   
69          return  this .allStudents ;
 70      }
 71      public String toString(){
 72          return "School name:" + this .name ;
 73      }
 74 }
复制代码

代码参考:https://www.cnblogs.com/xscn/archive/2013/08/09/3249274.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324575588&siteId=291194637