天天看點

one-to-one的了解

(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.    <class name="Topic" table="topic">
  15.        <id name="id" column="id" unsaved-value="null">
  16.            <generator class="sequence">
  17.               <param name="sequence">topic_id</param>
  18.            </generator>
  19.        </id>
  20.        <property name="topicname"/>
  21.        <one-to-one name="author" cascade="save-update"/>
  22.    </class>
  23. </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.       <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  12.       <!-- 配置資料庫連接配接 -->
  13.       <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  14.       <property name="connection.username">system</property>
  15.       <property name="connection.password">0</property>
  16.       <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
  17.       <!-- 配置連接配接池 -->
  18.       <property name="c3p0.max_size">2</property>
  19.       <property name="c3p0.min_size">2</property>
  20.       <property name="c3p0.timeout">5000</property>
  21.       <property name="c3p0.max_statements">100</property>
  22.       <property name="c3p0.idle_test_period">3000</property>
  23.       <property name="c3p0.acquire_increment">2</property>
  24.       <property name="c3p0.validate">false</property>
  25.       <!-- 指定hibernate管理的映射檔案 -->
  26.       <mapping resource="one_to_one/topic_author.hbm.xml"/>
  27.     </session-factory>
  28. </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.             a.setTopic(t);           
  22.             ses.save(a);
  23.             tc.commit();
  24.         } catch (HibernateException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.             if(tc!=null)tc.rollback();
  28.         }finally
  29.         {
  30.             if(ses!=null)ses.close();
  31.             if(sf!=null)sf.close();
  32.         }
  33.     }
  34. }

雙向關聯: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裡隻要按你自己想要存資料的方向來寫就行!