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

    1、Annotation 注解版  

    1.1、创建Husband类和Wife类

     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 
     8 import org.hibernate.annotations.Type;
     9 
    10 /**
    11  * @author DSHORE/2019-9-18
    12  * 一对一,单向关联(注解版)
    13  */
    14 @Entity
    15 public class Husband {//主表
    16     private Integer id;
    17     private String name;
    18     private Boolean sex;
    19 
    20     @Id
    21     @GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略(使用test1测试时,必须加上这个,否会报错:id生成错误)
    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     
    35     @Type(type = "yes_no") //数据库中,会以Y/N的形式插入到sex字段中
    36     public Boolean getSex() {
    37         return sex;
    38     }
    39     public void setSex(Boolean sex) {
    40         this.sex = sex;
    41     }
    42 }

    Wife类

     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.OneToOne;
     8 
     9 import org.hibernate.annotations.Type;
    10 
    11 /**
    12  * @author DSHORE/2019-9-18
    13  * 一对一,单向关联(注解版)
    14  */
    15 @Entity
    16 public class Wife {//从表
    17     private Integer id;
    18     private String name;
    19     private Boolean sex;
    20     private Husband husband;
    21 
    22     @Id
    23     @GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略(使用test1测试时,必须加上这个,否会报错:id生成错误)
    24     public Integer getId() {
    25         return id;
    26     }
    27     public void setId(Integer id) {
    28         this.id = id;
    29     }
    30     public String getName() {
    31         return name;
    32     }
    33     public void setName(String name) {
    34         this.name = name;
    35     }
    36     
    37     @Type(type = "yes_no") //数据库中,会以Y/N的形式插入到sex字段中
    38     public Boolean getSex() {
    39         return sex;
    40     }
    41     public void setSex(Boolean sex) {
    42         this.sex = sex;
    43     }
    44     
    45     @OneToOne  //默认创建的外键名称:husband_id
    46     //@JoinColumn(name="husbandId") //创建表时,指定该外键名:husbandId
    47     public Husband getHusband() {
    48         return husband;
    49     }
    50     public void setHusband(Husband husband) {
    51         this.husband = husband;
    52     }
    53 }

    1.2、创建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.Husband" />
    20         <mapping class="com.shore.model.Wife" />
    21     </session-factory>
    22 </hibernate-configuration>

    1.3、开始测试

     1 package com.shore.test;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.hibernate.Transaction;
     6 import org.hibernate.cfg.AnnotationConfiguration;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.tool.hbm2ddl.SchemaExport;
     9 import org.junit.AfterClass;
    10 import org.junit.BeforeClass;
    11 import org.junit.Test;
    12 
    13 import com.shore.model.Husband;
    14 import com.shore.model.Wife;
    15 
    16 /**
    17  * @author DSHORE/2019-9-18
    18  * 
    19  */
    20 public class AnnotationTest {
    21 /*    public static SessionFactory sessionFactory = null;
    22     public static Session session = null;
    23     
    24     @BeforeClass
    25     public static void buildSessionFactory() {
    26         sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    27     }
    28     
    29     @AfterClass
    30     public static void close() {
    31         session.close();
    32         sessionFactory.close();
    33     }
    34     
    35     @Test
    36     public void test1(){//数据库表创建完后,插入数据
    37         session = sessionFactory.openSession();
    38         Transaction transaction = session.beginTransaction();
    39         Husband husband = new Husband();
    40         husband.setName("黄晓明");
    41         husband.setSex(true);//男
    42         session.save(husband);
    43         
    44         Wife wife = new Wife();
    45         wife.setName("AnglaBaby");
    46         wife.setSex(false);//女
    47         wife.setHusband(husband);
    48         session.save(wife);
    49         transaction.commit();//事务提交
    50     }*/
    51     
    52     // 要进行save操作,先save(husband),后save(wife)
    53     @Test
    54     public void test2() {//只创建数据库表,不插入任何数据,可以这样测试。(hibernate.cfg.xml配置文件用的是create)
    55         new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    56     }
    57 }

    测试结果图:

         

     

    2、XML实现 版  

    2.1、创建husband类和wife类

     1 package com.shore.domel;
     2 
     3 /**
     4  * @author DSHORE/2019-9-18
     5  * 一对一,单向关联(xml版)
     6  */
     7 public class Husband {//主表
     8     private Integer id;
     9     private String name;
    10     private Boolean sex;
    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 Boolean getSex() {
    25         return sex;
    26     }
    27     public void setSex(Boolean sex) {
    28         this.sex = sex;
    29     }
    30 }

    wife类

     1 package com.shore.domel;
     2 
     3 /**
     4  * @author DSHORE/2019-9-18
     5  * 一对一,单向关联(xml版)
     6  */
     7 public class Wife {//从表
     8     private Integer id;
     9     private String name;
    10     private Boolean sex;
    11     private Husband husband;
    12 
    13     public Integer getId() {
    14         return id;
    15     }
    16     public void setId(Integer id) {
    17         this.id = id;
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public Boolean getSex() {
    26         return sex;
    27     }
    28     public void setSex(Boolean sex) {
    29         this.sex = sex;
    30     }
    31     public Husband getHusband() {
    32         return husband;
    33     }
    34     public void setHusband(Husband husband) {
    35         this.husband = husband;
    36     }
    37 }

    2.2、创建 Husband.hbm.xml 配置文件和 Wife.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.domel">
     7     <class name="Husband" table="husband_xml">  
     8         <id name="id"> 
     9             <generator class="native"/>
    10         </id>
    11         <property name="name" type="java.lang.String"/>
    12         <property name="sex" type="yes_no"/>
    13     </class>
    14 </hibernate-mapping>

    Wife.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.domel">
     7     <class name="Wife" table="wife_xml">  
     8         <id name="id"> 
     9             <generator class="native"/>
    10         </id>
    11         <property name="name" type="java.lang.String"/>
    12         <property name="sex" type="yes_no" />
    13         
    14         <!-- many-to-one:多对一,但加了个unique="true",就变成了一对一 -->
    15         <many-to-one name="husband" column="husband_id" unique="true"/>
    16     </class>
    17 </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.Husband" />
    20         <mapping class="com.shore.model.Wife" /> -->
    21         <mapping resource="com/shore/domel/Husband.hbm.xml" />
    22         <mapping resource="com/shore/domel/Wife.hbm.xml" />
    23     </session-factory>
    24 </hibernate-configuration>

    2.4、开始测试

     1 package com.shore.test;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.hibernate.Transaction;
     6 import org.hibernate.cfg.Configuration;
     7 import org.junit.AfterClass;
     8 import org.junit.BeforeClass;
     9 import org.junit.Test;
    10 
    11 import com.shore.domel.Husband;
    12 import com.shore.domel.Wife;
    13 
    14 /**
    15  * @author DSHORE/2019-9-18
    16  *
    17  */
    18 public class XMLTest1 {
    19     public static SessionFactory sessionFactory = null;
    20     public static Session session = null;
    21     
    22     @BeforeClass
    23     public static void buildSessionFactory() {
    24                   //用注解版的话,Configuration()方法,得改用AnnotationConfiguration()方法
    25         sessionFactory = new Configuration().configure().buildSessionFactory();
    26     }
    27     
    28     @AfterClass
    29     public static void close() {
    30         session.close();
    31         sessionFactory.close();
    32     }
    33     
    34     @Test
    35     public void test() {
    36         session = sessionFactory.openSession();
    37         Transaction transaction = session.beginTransaction();
    38         Husband husband = new Husband();
    39         husband.setName("黄晓明");
    40         husband.setSex(true);//
    41         session.save(husband);
    42         
    43         Wife wife = new Wife();
    44         wife.setName("AnglaBaby");
    45         wife.setSex(false);//
    46         wife.setHusband(husband);
    47         session.save(wife);
    48         transaction.commit();
    49     }
    50 }

    测试结果图:

         

     

    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/11545058.html

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

  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/dshore123/p/11545058.html
Copyright © 2011-2022 走看看