Hibernate實戰(二)

來源:互聯網
上載者:User
Hibernate實戰(二)
 Bromon原創 著作權
 one-to-one關係
 在絕大多數系統當中不可能只存在一個資料表格,否則就違背了關聯式資料庫的初衷。表與表的關係比較複雜,可以分為幾種情況:

 ● 一對一關聯(one to one)
 ● 一對多關聯(one to many)
 ● 多對一關聯(many to one)
 ● 多對多關聯(many to many)

 按順序來講。假設一個一對一關聯的例子是:
 表格:person
 id 編號(主鍵)
 name 姓名
 email email地址

 表格:spouse
 id 編號(外鍵)
 name 姓名

 person這個表儲存使用者資訊,而spouse儲存使用者配偶的資訊。在一般情況下一個人只有一個配偶,這很適合我們一對一的情況。如果你對婚外戀感興趣的話,我們可以在一對多和多對一的關聯中討論這個問題,也許還可以在多對多中^_^(禽獸!)。

 OK,下面設計POJO:
 Person這個類非常簡單:

 /*
 * Created on 2004-4-19
 */
 package org.bromon.zizz;

 /**
 * @author Bromon
 */
 public class Person
 {
 private int id;
 private String name;
 private String email;

 public void setId(int id)
 {
  this.id=id;
 }
 public int getId()
 {
  return(id);
 }

 public void setName(String name)
 {
  this.name=name;
 }
 public String getName()
 {
  return(name);
 }

 public void setEmail(String email)
 {
  this.email=email;
 }
 public String getEmail()
 {
  return(email);
 }
 } 

 然後編寫它的映射規則,這個應該能夠理解了:
 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
 "' target=_blank>http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">  

 <hibernate-mapping package="org.bromon.zizz">
 <class name="Person" table="person" lazy="true">
 <id name="id" type="integer" unsaved-value="null">
 <column name="id" sql-type="int" not-null="true"/>
 <generator class="identity"/>
 </id>

 <property name="name" column="name" not-null="true" unique="false"/>
 <property name="email" column="email" not-null="false"/>
 </class> 
 </hibernate-mapping> 

 so easy是不是?一切都按部就班。下面是Souse類:

 /*
 * Created on 2004-4-20
 */
 package org.bromon.zizz;

 /**
 * @author Bromon
 */
 public class Spouse
 {
 private int id;
 private String name;
 private Person person;

 public void setId(int id)
 {
  this.id=id;
 }
 public int getId()
 {
  return(id);
 }

 public void setName(String name)
 {
  this.name=name;
 }
 public String getName()
 {
  return(name);
 }

 public void setPerson(Person person)
 {
  this.person=person;
 }
 public Person getPerson()
 {
  return(person);
 }
 } 

 注意裡面的域person。它的對應檔:

 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
 "' target=_blank>http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">  

 <hibernate-mapping package="org.bromon.zizz">
 <class name="Spouse" table="spouse" lazy="true">
 <id name="id" type="integer" unsaved-value="null">
 <column name="id" sql-type="int" not-null="true"/>
 <generator class="foreign"> 
 <param name="property">person</param> 
 </generator> 
 </id>

 <property name="name" column="name" not-null="true" unique="false"/>
 <one-to-one name="person" class="Person" cascade="all" constrained="true" />
 </class> 
 </hibernate-mapping> 

 這裡指明了id的generator是一個外鍵,和person相關聯。然後指定一個one-to-one關係,不難理解是不是?Hibernate的確很符合我們的思維習慣。需要提醒的是,這種關聯關係是單向的,Person並不需要去指定Spouse。

 下面來操作這兩個類:

 /*
 * Created on 2004-4-20
 */
 package org.bromon.zizz;
 import net.sf.hibernate.*;
 import net.sf.hibernate.cfg.*;
 import net.sf.hibernate.tool.hbm2ddl.*;
 /**
 * @author Bromon
 */
 public class OperateSpouse
 {
 public static void main(String args[])
 {
  try
  {
   Configuration cfg=new Configuration().addClass(Spouse.class);
   cfg.addClass(Person.class);
   SessionFactory factory=cfg.buildSessionFactory();
   new SchemaExport(cfg).create(true,true);
   Session session=factory.openSession();
  
   Person person=new Person();
   person.setName("bromon");
   person.setEmail("bromon@163.com");
  
   Spouse spouse=new Spouse();
   spouse.setName("who");
   spouse.setPerson(person);
   
   Transaction ts=session.beginTransaction();
   session.save(person);
   session.save(spouse);
   ts.commit();
   session.close();
  }catch(Exception e)
  {
   System.out.println(e);
  }
 } 
 } 

 這個例子和第一篇中的例子非常相似。OK,執行一下,然後看看zizz資料庫,搞掂。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.