11 Hibernate Java knowledge-many unidirectional association (Annotation + XML achieve)

1, Annotation version comment  

1.1 application scenarios (Student-Teacher): When students know what teachers teach, but do not know what the teacher taught the students themselves, it can be associated with one-way-many mode

1.2, create a class Teacher and Student classes

 1 package com.shore.model;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 import javax.persistence.Table;
 8 
 9 /**
10  * @author DSHORE/2019-9-22
11  * 多对多,单向关联(注解版)
12  */
13 @Entity
14 @Table(name="anno_teacher")
15 public class Teacher {
16     private Integer id;
17     private String name;
18     private Integer age;
19     
20     @Id
21     @GeneratedValue(strategy=GenerationType.IDENTITY)
22     public Integer getId() {
23         return id;
24     }
25     public void setId(Integer id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public Integer getAge() {
35         return age;
36     }
37     public void setAge(Integer age) {
38         this.age = age;
39     }
40 }

Student category

 1 package com.shore.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.GenerationType;
 9 import javax.persistence.Id;
10 import javax.persistence.JoinColumn;
11 import javax.persistence.JoinTable;
12 import javax.persistence.ManyToMany;
13 import javax.persistence.Table;
14 
15 /**
16  * @author DSHORE/2019-9-22
17  * 多对一,单向关联(注解版)
18  */
19 @Entity
20 @Table(name="anno_student") 
21 public class Student {
22     private Integer id;
23     private String number;
24     private Float sum;
25     private Set<Teacher> teachers = new HashSet<Teacher>();
26     
27     @Id
28     @GeneratedValue(strategy=GenerationType.IDENTITY)
29     public Integer getId() {
30         return id;
31     }
32     public void setId(Integer id) {
33         this.id = id;
34     }
35     public String getNumber() {
36         return number;
37     }
38     public void setNumber(String number) {
39         this.number = number;
40     }
41     public Float getSum() {
42         return sum;
43     }
44     public  void setSum (the Float SUM) {
 45          the this .sum = SUM;
 46 is      }
 47      
48      / ** 
49       * name: the name of the middle table in Table
 50       * joinColumns: corresponding to the current object ID
 51 is       * inverseJoinColumns: the other object corresponds ID
 52 is       * / 
53 is      @ManyToMany
 54 is      @JoinTable (name = "anno_student_teacher" ,
 55                 joinColumns of JoinColumn @ = (name = "the student_id" ),
 56 is                 inverseJoinColumns of JoinColumn @ = (name = "teacher_id" ))
 57 is      public the Set <Teacher> getTeachers () {
58         return teachers;
59     }
60     public void setTeachers(Set<Teacher> teachers) {
61         this.teachers = teachers;
62     }
63 }

1.3, creates the hibernate.cfg.xml core profile

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- Database connection settings -->
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
11         <property name="connection.username">root</property>
12         <property name="connection.password">root</property>
13 
14         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
15         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
16         <property name="show_sql">true</property>
17         <property name="hbm2ddl.auto">create</property>
18 
19         <mapping class="com.shore.model.Teacher" />
20         <mapping class="com.shore.model.Student" />
21     </session-factory>
22 </hibernate-configuration>

1.4, start the test

. 1  Package com.shore.test;
 2  
. 3  Import org.hibernate.cfg.AnnotationConfiguration;
 . 4  Import org.hibernate.tool.hbm2ddl.SchemaExport;
 . 5  Import org.junit.Test;
 . 6  
. 7  / ** 
. 8  * @author DSHORE / 2019-9-22
 . 9  *
 10   * / 
. 11  public  class AnnotationTest {
 12 is      @Test
 13 is      public  void test () { // simple test, only the table created, the data is not inserted 
14          new new the SchemaExport ( new new AnnotationConfiguration().configure()).create(
15                 false, true);
16     }
17 }

Test results chart:

 

    

 

 

Guess you like

Origin www.cnblogs.com/dshore123/p/11568536.html