zoukankan      html  css  js  c++  java
  • Hibernate学习笔记3.3(Hibernate组建映射2)

    多对多

    相当于一个老师可以教多个学生,一个学生也可以有多个老师

    数据表中都是再设计一个表寸相关的id

    1.多对多单向

    1annotation

    Student.java

    package com.bjsxt.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    @Entity
    public class Student {
        private int id;
        private String name;
        
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    teacher.java

    package com.bjsxt.hibernate;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    @Entity
    public class Teacher {
        private int id;
        private String name;
        private Set<Student> students = new HashSet<Student>();
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @ManyToMany
        @JoinTable(name="t_s",
            joinColumns={@JoinColumn(name="teacher_id")},    //自己这个类的外键id
            inverseJoinColumns={@JoinColumn(name="student_id")}   //对方那张表的外键id
            )
        public Set<Student> getStudents() {
            return students;
        }
        public void setStudents(Set<Student> students) {
            this.students = students;
        }
    }

    hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
        <session-factory>
    
             
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">bjsxt</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
              <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup
            <property name="hbm2ddl.auto">update</property>
             -->
            
            <mapping class="com.bjsxt.hibernate.Teacher"/>
            <mapping class="com.bjsxt.hibernate.Student"/>
            
            
            <!--  
             <mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/>
             <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
              -->
        </session-factory>
    
    </hibernate-configuration>

    测试代码

    package com.bjsxt.hibernate;
    
    import java.util.Date;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class HibernateORMappingTest {
        private static SessionFactory sessionFactory;
        
        //@BeforeClass
        public static void beforeClass() {
                sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        //@AfterClass
        public static void afterClass() {
            sessionFactory.close();
        }
        
        
        
        @Test
        public void testSchemaExport() {
            new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        }
        
        public static void main(String[] args) {
            beforeClass();
        }
    }

    2.xml

    student.hbm.xml

    <?xml version="1.0"?>
    <!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.bjsxt.hibernate.Student">
            <id name="id">
                <generator class="native"></generator>
            </id>
            
            <property name="name"></property>
        </class>
        
    </hibernate-mapping>

    teacher.hbm.xml

    <?xml version="1.0"?>
    <!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.bjsxt.hibernate.Student">
            <id name="id">
                <generator class="native"></generator>
            </id>
            
            <property name="name"></property>
        </class>
        
    </hibernate-mapping>

    hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
        <session-factory>
    
             
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">bjsxt</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
              <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup
            <property name="hbm2ddl.auto">update</property>
             -->
            <!--  
            <mapping class="com.bjsxt.hibernate.Teacher"/>
            <mapping class="com.bjsxt.hibernate.Student"/>
             -->
            
            
             <mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/>
             <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
             
        </session-factory>
    
    </hibernate-configuration>

     多对多双向关联

    1.annotation

    student.java

    package com.bjsxt.hibernate;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToMany;
    
    @Entity
    public class Student {
        private int id;
        private String name;
        private Set<Teacher> teachers = new HashSet<Teacher>();
        @ManyToMany(mappedBy="students")
        public Set<Teacher> getTeachers() {
            return teachers;
        }
        public void setTeachers(Set<Teacher> teachers) {
            this.teachers = teachers;
        }
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    Teacher.java

    package com.bjsxt.hibernate;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    @Entity
    public class Teacher {
        private int id;
        private String name;
        private Set<Student> students = new HashSet<Student>();
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @ManyToMany
        @JoinTable(name="t_s",
            joinColumns={@JoinColumn(name="teacher_id")},
            inverseJoinColumns={@JoinColumn(name="student_id")}
            )
        public Set<Student> getStudents() {
            return students;
        }
        public void setStudents(Set<Student> students) {
            this.students = students;
        }
    }

    hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
        <session-factory>
    
             
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">bjsxt</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
              <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup
            <property name="hbm2ddl.auto">update</property>
             -->
          
           
              <mapping class="com.bjsxt.hibernate.Teacher"/>
            <mapping class="com.bjsxt.hibernate.Student"/>
      <!--  
    <mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/> <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
     -->
            
    </session-factory> </hibernate-configuration>

    test.java

    package com.bjsxt.hibernate;
    
    import java.util.Date;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class HibernateORMappingTest {
        private static SessionFactory sessionFactory;
        
        //@BeforeClass
        public static void beforeClass() {
                sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        //@AfterClass
        public static void afterClass() {
            sessionFactory.close();
        }
        
        
        
        @Test
        public void testSchemaExport() {
            new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        }
        
        public static void main(String[] args) {
            beforeClass();
        }
    }

    2。xml

    Student.hbm.xml

    <?xml version="1.0"?>
    <!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.bjsxt.hibernate.Student">
            <id name="id">
                <generator class="native"></generator>
            </id>
            
            <property name="name"></property>
            <set name="teachers" table="t_s">
                <key column="student_id"></key>
                <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
            </set>
        </class>
        
    </hibernate-mapping>

    Teacher.hbm.xml

    <?xml version="1.0"?>
    <!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.bjsxt.hibernate.Teacher">
            <id name="id">
                <generator class="native"></generator>
            </id>
            
            <property name="name"></property>
            <set name="students" table="t_s">
                <key column="teacher_id"></key>
                <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
            </set>
        </class>
        
    </hibernate-mapping>

    hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
        <session-factory>
    
             
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">bjsxt</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
           <!--
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
              <property name="dialect">org.hibernate.dialect.OracleDialect</property>
           -->
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup
            <property name="hbm2ddl.auto">update</property>
             -->
            <!--  
           
              <mapping class="com.bjsxt.hibernate.Teacher"/>
            <mapping class="com.bjsxt.hibernate.Student"/>
            
             -->
            
            
            <mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/>
             <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
        </session-factory>
    
    </hibernate-configuration>

    test一致

  • 相关阅读:
    JDK所有版本
    application.yml配置log日志
    eclipse配置lombok
    Eclipse配置springboot
    java 连接mongodb
    MongoDB shell操作
    mysql插入一万条数据
    Web设计精髓(转)
    SyntaxHighlighter -- 代码高亮插件
    input之placeholder与行高的问题。
  • 原文地址:https://www.cnblogs.com/frankzone/p/9449823.html
Copyright © 2011-2022 走看看