zoukankan      html  css  js  c++  java
  • Java进阶知识10 Hibernate一对多单向关联(Annotation+XML实现)

    1、Annotation 注解版  

    1.1、在一的一方加Set

    1.2、创建Customer类和Order类

     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.Id;
     8 import javax.persistence.JoinColumn;
     9 import javax.persistence.OneToMany;
    10 import javax.persistence.Table;
    11 
    12 /**
    13  * @author DSHORE/2019-9-19
    14  * 一对多,单向关联(注解版)
    15  */
    16 @Entity
    17 @Table(name="anno_customer")
    18 public class Customer {//顾客  (“一”的一方),在“一”的一方加Set
    19     private Integer id;
    20     private String name;
    21     private Integer age;
    22     private Set<Order> orders = new HashSet<Order>();  //在“一”的一方加Set
    23     /**
    24      * List 有序,可重复,可以用index取值(get(index))
    25      * Set  无序,不可重复
    26      */
    27     
    28     @Id
    29     public Integer getId() {
    30         return id;
    31     }
    32     public void setId(Integer id) {
    33         this.id = id;
    34     }
    35     public String getName() {
    36         return name;
    37     }
    38     public void setName(String name) {
    39         this.name = name;
    40     }
    41     public Integer getAge() {
    42         return age;
    43     }
    44     public void setAge(Integer age) {
    45         this.age = age;
    46     }
    47     
    48     @OneToMany  //一对多
    49     @JoinColumn(name="customerId")
    50     public Set<Order> getOrders() {//在“一”的一方加Set
    51         return orders;
    52     }
    53     public void setOrders(Set<Order> orders) {
    54         this.orders = orders;
    55     }
    56 }

    Order类

     1 package com.shore.model;
     2 
     3 import javax.persistence.Entity;
     4 import javax.persistence.Id;
     5 import javax.persistence.Table;
     6 
     7 /**
     8  * @author DSHORE/2019-9-19
     9  *  一对多,单向关联(注解版)
    10  */
    11 @Entity
    12 @Table(name="anno_order") //Order是MySQL数据库关键字。需重新定义表名
    13 public class Order {//订单  (“多”的一方)
    14     private Integer id;
    15     private String number;
    16     private Float sum;
    17     
    18     @Id
    19     public Integer getId() {
    20         return id;
    21     }
    22     public void setId(Integer id) {
    23         this.id = id;
    24     }
    25     public String getNumber() {
    26         return number;
    27     }
    28     public void setNumber(String number) {
    29         this.number = number;
    30     }
    31     public Float getSum() {
    32         return sum;
    33     }
    34     public void setSum(Float sum) {
    35         this.sum = sum;
    36     }
    37 }

    1.3、创建hibernate.cfg.xml核心配置文件

     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">123456</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.Customer" />
    20         <mapping class="com.shore.model.Order" />
    21         <!-- <mapping resource="com/shore/model/Customer.hbm.xml" />
    22         <mapping resource="com/shore/model/Order.hbm.xml" /> -->
    23     </session-factory>
    24 </hibernate-configuration>

    1.4、开始测试

     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-19
     9  *
    10  */
    11 public class AnnotationTest {
    12     @Test
    13     public void test() {//简单测试,只创建表,不插入数据
    14                     //注解版,用AnnotationConfiguration()方法
    15         new SchemaExport(new AnnotationConfiguration().configure()).create(
    16                 false, true);
    17     }
    18 }

    测试结果图:

         

     

    2、XML版 的实现  

    2.1、创建Customer类和Order类

     1 package com.shore.model;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 /**
     7  * @author DSHORE/2019-9-19
     8  * 一对多,单向关联(xml版)
     9  */
    10 public class Customer {//顾客  (“一”的一方),在一的一方加Set
    11     private Integer id;
    12     private String name;
    13     private Integer age;
    14     private Set<Order> orders = new HashSet<Order>(); //在“一”的一方加Set
    15     
    16     public Integer getId() {
    17         return id;
    18     }
    19     public void setId(Integer id) {
    20         this.id = id;
    21     }
    22     public String getName() {
    23         return name;
    24     }
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28     public Integer getAge() {
    29         return age;
    30     }
    31     public void setAge(Integer age) {
    32         this.age = age;
    33     }
    34     public Set<Order> getOrders() {
    35         return orders;
    36     }
    37     public void setOrders(Set<Order> orders) {
    38         this.orders = orders;
    39     }
    40 }

    Order类

     1 package com.shore.model;
     2 
     3 /**
     4  * @author DSHORE/2019-9-19
     5  * 一对多,单向关联(xml版)
     6  */
     7 public class Order {//订单  (“多”的一方)
     8     private Integer id;
     9     private String number;
    10     private Float sum;
    11     
    12     public Integer getId() {
    13         return id;
    14     }
    15     public void setId(Integer id) {
    16         this.id = id;
    17     }
    18     public String getNumber() {
    19         return number;
    20     }
    21     public void setNumber(String number) {
    22         this.number = number;
    23     }
    24     public Float getSum() {
    25         return sum;
    26     }
    27     public void setSum(Float sum) {
    28         this.sum = sum;
    29     }
    30 }

    2.2、创建 Customer.hbm.xml 配置文件和 Order.hbm.xml 配置文件

     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="Customer" table="customer_xml">  
     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         
    14         <!-- 一对多,在“一”的一方的配置文件中用set标签 -->
    15         <set name="orders">
    16             <key column="customerId"></key>
    17             <one-to-many class="com.shore.model.Order"/>
    18         </set>
    19     </class>
    20 </hibernate-mapping>

    Order.hbm.xml 配置文件

     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="Order" table="order_xml">  
     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     </class>
    14 </hibernate-mapping>

    2.3、创建hibernate.cfg.xml 核心配置文件

     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">123456</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.Customer" />
    20         <mapping class="com.shore.model.Order" /> -->
    21         <mapping resource="com/shore/model/Customer.hbm.xml" />
    22         <mapping resource="com/shore/model/Order.hbm.xml" />
    23     </session-factory>
    24 </hibernate-configuration>

    2.4、开始测试

     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-19
     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 }

    测试结果图:

         

     

    Hibernate一对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545058.html
    Hibernate一对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545077.html

    Hibernate多对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553213.html
    Hibernate一对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553215.html
    Hibernate一对多多对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11560433.html

    Hibernate多对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568536.html
    Hibernate多对多双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568963.html

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:https://www.cnblogs.com/dshore123/p/11553215.html

    版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

  • 相关阅读:
    PHP+MySQL
    Appstore排名前十的程序员应用软件
    架构师的平凡之路
    程序员,如何三十而立?
    不懂技术也可以轻松开发一款APP
    php语法学习:轻松看懂PHP语言
    你真的了解软件测试行业吗?
    十个程序员必备的网站推荐
    从更高点看软件开发的侧重点
    php如何实现文件下载
  • 原文地址:https://www.cnblogs.com/dshore123/p/11553215.html
Copyright © 2011-2022 走看看