one-to-one的理解

来源:互联网 发布:淘宝换货直接寄过去 编辑:程序博客网 时间:2024/05/01 12:44
(Eclipse平台、OracleXe数据库)

在阐述关联关系以前,先说下:

one-to-one有两种方式:共享主键、唯一外键

单向关联:只有一个JavaBean里面有另一个对象引用属性。(我中有你,你中没我)

双向关联:彼此都有彼此的对象引用属性。(我中有你,你中有我)

单向关联one-to-one关系:(以author---topic为列)

 

======   topic_author.sql  =======(建表)

  1. create sequence topic_id;
  2. create sequence author_id;
  3. create table topic
  4. (id number(10) primary key,
  5.  topicname varchar2(20) not null
  6. );
  7. create table author
  8. (id number(10) primary key,
  9.  name varchar2(20) not null);

======   Author.java(JavaBean部分) =====

  1. package one_to_one;
  2. public class Author {
  3.      private Integer id;
  4.      private String name;
  5.      public Author(){}
  6.      public Author(String name)
  7.      {
  8.          this.name=name;
  9.      }
  10.      public void setId(Integer id)
  11.      {
  12.          this.id=id;
  13.      }
  14.      public Integer getId()
  15.      {
  16.          return this.id;
  17.      }
  18.      public void setName(String name)
  19.      {
  20.          this.name=name;
  21.      }
  22.      public String getName()
  23.      {
  24.          return this.name;
  25.      }
  26. }

======   Topic.java(JavaBean部分) =====

  1. package one_to_one;
  2. public class Topic {
  3.     private Integer id;
  4.     private String topicname;
  5.     private Author author;
  6.     public void setAuthor(Author author) //另类构造方法(简针对关联关系)
  7.     {
  8.         this.author=author;
  9.     }
  10.     public Author getAuthor()
  11.     {
  12.         return this.author;
  13.     }
  14.     public Topic(){}
  15.     public Topic(String topicname)
  16.     {
  17.         this.topicname=topicname;
  18.     }
  19.     public void setId(Integer id)
  20.     {
  21.         this.id=id;
  22.     }
  23.     public Integer getId()
  24.     {
  25.         return this.id;
  26.     }
  27.     public void setTopicname(String topicname)
  28.     {
  29.         this.topicname=topicname;
  30.     }
  31.     public String getTopicname()
  32.     {
  33.         return this.topicname;
  34.     }
  35. }

======   topic_author.hbm.xml (映射文件) =========

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="one_to_one">
  6.    <class name="Author" table="author">
  7.        <id name="id" column="id" unsaved-value="null">
  8.           <generator class="sequence">
  9.              <param name="sequence">author_id</param>
  10.           </generator>
  11.        </id>
  12.        <property name="name"/>
  13.    </class>
  14.   
  15.    <class name="Topic" table="topic">
  16.        <id name="id" column="id" unsaved-value="null">
  17.            <generator class="sequence">
  18.               <param name="sequence">topic_id</param>
  19.            </generator>
  20.        </id>
  21.        <property name="topicname"/>
  22.        <one-to-one name="author" cascade="save-update"/>
  23.    </class>
  24. </hibernate-mapping>

======  hibernatev.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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6.     <session-factory>
  7.       <!-- 显示SQL -->
  8.       <property name="show_sql">true</property>
  9.       <property name="format_sql">true</property>
  10.       
  11.       <!-- 配置数据库方言 -->
  12.       <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  13.       
  14.       <!-- 配置数据库连接 -->
  15.       <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  16.       <property name="connection.username">system</property>
  17.       <property name="connection.password">0</property>
  18.       <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
  19.      
  20.       <!-- 配置连接池 -->
  21.       <property name="c3p0.max_size">2</property>
  22.       <property name="c3p0.min_size">2</property>
  23.       <property name="c3p0.timeout">5000</property>
  24.       <property name="c3p0.max_statements">100</property>
  25.       <property name="c3p0.idle_test_period">3000</property>
  26.       <property name="c3p0.acquire_increment">2</property>
  27.       <property name="c3p0.validate">false</property>
  28.       
  29.       <!-- 指定hibernate管理的映射文件 -->
  30.       <mapping resource="one_to_one/topic_author.hbm.xml"/>
  31.       
  32.     </session-factory>
  33. </hibernate-configuration>

========   Test.java (测试类)  ========

 

 

  1. package one_to_one;
  2. import org.hibernate.HibernateException;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. import oto.Car;
  7. import oto.License;
  8. public class Test {
  9.     public static void main(String[] args)
  10.     {
  11.         Configuration cfg=new Configuration().configure();
  12.         SessionFactory sf=null;
  13.         Session ses=null;
  14.         org.hibernate.Transaction tc=null;
  15.         try {
  16.             sf=cfg.buildSessionFactory();
  17.             ses=sf.openSession();
  18.             tc=ses.beginTransaction();
  19.             Author a=new Author("bai");
  20.             Topic t=new Topic("fffff");
  21.             
  22.             a.setTopic(t);           
  23.             ses.save(a);
  24.             
  25.             tc.commit();
  26.         } catch (HibernateException e) {
  27.             // TODO Auto-generated catch block
  28.             e.printStackTrace();
  29.             if(tc!=null)tc.rollback();
  30.         }finally
  31.         {
  32.             if(ses!=null)ses.close();
  33.             if(sf!=null)sf.close();
  34.         }
  35.         
  36.     }
  37. }

双向关联:one-to-one

只需改动author.java   和 topic_author.hbm.xml 。Test.java

在author.java   中,在private String name行和构造方法之间,加上如下代码:

private Topic topic;
     public void setTopic(Topic topic)
     {
      this.topic=topic;
     }
     public Topic getTopic()
     {
      return this.topic;
     }

在topic_author.hbm.xml 中,在author的</class>标签前面加上一行:

<one-to-one name="topic" cascade="save-update"/>

 在test.java里只要按你自己想要存数据的方向来写就行!