Java知识点11 Hibernate多对多单向关联(Annotation+XML实现)
2020-12-13 14:42
标签:单向关联 coding his com 原创 自己 teacher driver figure 1.1、应用场景(Student-Teacher):当学生知道有哪些老师教,但是老师不知道自己教哪些学生时,可用单向关联的多对多模式 1.2、创建Teacher类和Student类 Student类 1.3、创建hibernate.cfg.xml核心配置文件 1.4、开始测试 测试结果图: 2.1、创建Teacher类和Student类 Student类 2.2、创建 Teacher.hbm.xml 配置文件和 Student.hbm.xml 配置文件 Student.hbm.xml 配置文件 2.3、创建hibernate.cfg.xml 核心配置文件 2.4、开始测试 测试结果图: 原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/11568536.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) Java知识点11 Hibernate多对多单向关联(Annotation+XML实现) 标签:单向关联 coding his com 原创 自己 teacher driver figure 原文地址:https://www.cnblogs.com/dshore123/p/11568536.html1、Annotation 注解版
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 }
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
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
9 property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
10 property name="connection.url">jdbc:mysql://localhost:3306/hibernateproperty>
11 property name="connection.username">rootproperty>
12 property name="connection.password">123456property>
13
14 property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
15 property name="cache.provider_class">org.hibernate.cache.NoCacheProviderproperty>
16 property name="show_sql">trueproperty>
17 property name="hbm2ddl.auto">createproperty>
18
19 mapping class="com.shore.model.Teacher" />
20 mapping class="com.shore.model.Student" />
21 session-factory>
22 hibernate-configuration>
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 @Test
13 public void test() {//简单测试,只创建表,不插入数据
14 new SchemaExport(new AnnotationConfiguration().configure()).create(
15 false, true);
16 }
17 }
2、XML版 的实现
1 package com.shore.model;
2
3 /**
4 * @author DSHORE/2019-9-22
5 * 多对多,单向关联(xml版)
6 */
7 public class Teacher {
8 private Integer id;
9 private String name;
10 private Integer age;
11
12 public Integer getId() {
13 return id;
14 }
15 public void setId(Integer id) {
16 this.id = id;
17 }
18 public String getName() {
19 return name;
20 }
21 public void setName(String name) {
22 this.name = name;
23 }
24 public Integer getAge() {
25 return age;
26 }
27 public void setAge(Integer age) {
28 this.age = age;
29 }
30 }
1 package com.shore.model;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 /**
7 * @author DSHORE/2019-9-22
8 * 多对一,单向关联(xml版)
9 */
10 public class Student {
11 private Integer id;
12 private String number;
13 private Float sum;
14 private Set
1 xml version="1.0"?>
2 DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6 hibernate-mapping package="com.shore.model">
7 class name="Teacher" table="xml_teacher">
8 id name="id">
9 generator class="native"/>
10 id>
11 property name="name" type="java.lang.String"/>
12 property name="age" type="java.lang.Integer"/>
13 class>
14 hibernate-mapping>
1 xml version="1.0"?>
2 DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6 hibernate-mapping package="com.shore.model">
7 class name="Student" table="xml_student">
8 id name="id">
9 generator class="native"/>
10 id>
11 property name="number" type="java.lang.String"/>
12 property name="sum" type="java.lang.Float"/>
13
14 set name="teachers" table="xml_student_teacher">
15 key column="student_id"/>
16 many-to-many class="com.shore.model.Teacher" column="teacher_id"/>
17 set>
18 class>
19 hibernate-mapping>
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
9 property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
10 property name="connection.url">jdbc:mysql://localhost:3306/hibernateproperty>
11 property name="connection.username">rootproperty>
12 property name="connection.password">123456property>
13
14 property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
15 property name="cache.provider_class">org.hibernate.cache.NoCacheProviderproperty>
16 property name="show_sql">trueproperty>
17 property name="hbm2ddl.auto">createproperty>
18
19
21 mapping resource="com/shore/model/Teacher.hbm.xml" />
22 mapping resource="com/shore/model/Student.hbm.xml" />
23 session-factory>
24 hibernate-configuration>
1 package com.shore.test;
2
3 import org.hibernate.cfg.Configuration;
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 XMLTest {
12 @Test
13 public void test() {//简单测试,只创建表,不插入数据
14 //xml版,这里用的是Configuration()方法。
15 new SchemaExport(new Configuration().configure()).create(
16 false, true);
17 }
18 }
文章标题:Java知识点11 Hibernate多对多单向关联(Annotation+XML实现)
文章链接:http://soscw.com/essay/34390.html