(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里只要按你自己想要存数据的方向来写就行!