歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Hibernate關聯映射 ---一對一案例分析(基於外鍵)

一  概念分析

1.以Person – IdCard為例,Person為主,IdCard為從

    一個人只有一個身份證,一個身份證只屬於一個人

基於外鍵的是在從的那一方設置外鍵並增加一列

二 代碼分析

1.Person實例類

package com.hbsi.domain;

public class Person {

    private int id;

    private String name;

    private IdCard idCard; //一個人對應一個身份證

    public Person() {

       super();

       // TODO Auto-generated constructor stub

    }

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public IdCard getIdCard() {

       return idCard;

    }

    public void setIdCard(IdCard idCard) {

       this.idCard = idCard;

    }

}

2.Student實例類

package com.hbsi.domain;

import java.util.Date;

//基於外鍵的one to  one

public class IdCard {

    private int id;

    private Date usefulLife;

    private Person person;//一個身份證只屬於一個人

 

    public IdCard() {

       super();

       // TODO Auto-generated constructor stub

    }

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public Date getUsefulLife() {

       return usefulLife;

    }

    public void setUsefulLife(Date usefulLife) {

       this.usefulLife = usefulLife;

    }

    public Person getPerson() {

       return person;

    }

    public void setPerson(Person person) {

       this.person = person;

    }

}

3.Person的映射文件  Person.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

    <class name="Person" table="person">

       <id name="id" column="id">

           <generator class="native" />

       </id>

       <property name="name" column="name" />

       <!--property-ref 屬性的參照,即參照的是person類中的-->

       <one-to-one name="idCard" property-ref="person"></one-to-one>

    </class>

</hibernate-mapping>

4.IdCard類的映射文件

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hbsi.domain">

    <class name="IdCard" table="id_card">

       <id name="id" column="id">

           <generator class="native" />

       </id>

       <property name="usefulLife" column="useful_life" />

       <!-- 基於外鍵的一對一的映射  和多對一一樣,只是多對一可以有重復的部門,而人不可以有重復的身份證,所以要加上unique -->

       <many-to-one name="person" column="person_id" unique="true"></many-to-one>

    </class>

</hibernate-mapping>

5.測試

public class One2One {

    public static void main(String[] args) {

       add();

       query(1);

    }

//保存

    static Person add() {

       Session session = null;

       Transaction transaction = null;

       try {

           session = HibernateUtil.getSession();

           transaction = session.beginTransaction();

 

           IdCard idCard = new IdCard();

           idCard.setUsefulLife(new Date());

 

           Person person = new Person();

           person.setName("老王");

 

           idCard.setPerson(person);

           // person.setIdCard(idCard);

 

           session.save(person);

           session.save(idCard);

 

           transaction.commit();

           return person;

       } finally {

           if (session != null) {

              session.close();

           }

       }

    }

    //查詢

    static Person query(int person_id) {

       Session session = null;

       try {

           session = HibernateUtil.getSession();

 

           Person person = (Person) session.get(Person.class, person_id);

           System.out.println(person.getIdCard().getUsefulLife());

 

           return person;

       } finally {

           if (session != null) {

              session.close();

           }

       }

    }

}

Copyright © Linux教程網 All Rights Reserved