(Eclipse平台、OracleXe資料庫)
在闡述關聯關系以前,先說下:
one-to-one有兩種方式:共享主鍵、唯一外鍵
單向關聯:隻有一個JavaBean裡面有另一個對象引用屬性。(我中有你,你中沒我)
雙向關聯:彼此都有彼此的對象引用屬性。(我中有你,你中有我)
單向關聯one-to-one關系:(以author---topic為列)
====== topic_author.sql =======(建表)
- create sequence topic_id;
- create sequence author_id;
- create table topic
- (id number(10) primary key,
- topicname varchar2(20) not null
- );
- create table author
- (id number(10) primary key,
- name varchar2(20) not null);
====== Author.java(JavaBean部分) =====
- package one_to_one;
- public class Author {
- private Integer id;
- private String name;
- public Author(){}
- public Author(String name)
- {
- this.name=name;
- }
- public void setId(Integer id)
- {
- this.id=id;
- }
- public Integer getId()
- {
- return this.id;
- }
- public void setName(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return this.name;
- }
- }
====== Topic.java(JavaBean部分) =====
- package one_to_one;
- public class Topic {
- private Integer id;
- private String topicname;
- private Author author;
- public void setAuthor(Author author) //另類構造方法(簡針對關聯關系)
- {
- this.author=author;
- }
- public Author getAuthor()
- {
- return this.author;
- }
- public Topic(){}
- public Topic(String topicname)
- {
- this.topicname=topicname;
- }
- public void setId(Integer id)
- {
- this.id=id;
- }
- public Integer getId()
- {
- return this.id;
- }
- public void setTopicname(String topicname)
- {
- this.topicname=topicname;
- }
- public String getTopicname()
- {
- return this.topicname;
- }
- }
====== topic_author.hbm.xml (映射檔案) =========
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="one_to_one">
- <class name="Author" table="author">
- <id name="id" column="id" unsaved-value="null">
- <generator class="sequence">
- <param name="sequence">author_id</param>
- </generator>
- </id>
- <property name="name"/>
- </class>
- <class name="Topic" table="topic">
- <id name="id" column="id" unsaved-value="null">
- <generator class="sequence">
- <param name="sequence">topic_id</param>
- </generator>
- </id>
- <property name="topicname"/>
- <one-to-one name="author" cascade="save-update"/>
- </class>
- </hibernate-mapping>
====== hibernatev.cfg.xml (配置檔案)========
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- 顯示SQL -->
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <!-- 配置資料庫方言 -->
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <!-- 配置資料庫連接配接 -->
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="connection.username">system</property>
- <property name="connection.password">0</property>
- <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
- <!-- 配置連接配接池 -->
- <property name="c3p0.max_size">2</property>
- <property name="c3p0.min_size">2</property>
- <property name="c3p0.timeout">5000</property>
- <property name="c3p0.max_statements">100</property>
- <property name="c3p0.idle_test_period">3000</property>
- <property name="c3p0.acquire_increment">2</property>
- <property name="c3p0.validate">false</property>
- <!-- 指定hibernate管理的映射檔案 -->
- <mapping resource="one_to_one/topic_author.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
======== Test.java (測試類) ========
- package one_to_one;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import oto.Car;
- import oto.License;
- public class Test {
- public static void main(String[] args)
- {
- Configuration cfg=new Configuration().configure();
- SessionFactory sf=null;
- Session ses=null;
- org.hibernate.Transaction tc=null;
- try {
- sf=cfg.buildSessionFactory();
- ses=sf.openSession();
- tc=ses.beginTransaction();
- Author a=new Author("bai");
- Topic t=new Topic("fffff");
- a.setTopic(t);
- ses.save(a);
- tc.commit();
- } catch (HibernateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- if(tc!=null)tc.rollback();
- }finally
- {
- if(ses!=null)ses.close();
- if(sf!=null)sf.close();
- }
- }
- }
雙向關聯: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裡隻要按你自己想要存資料的方向來寫就行!