hibernate 一對一關係

一對一的另外一種解決方式就是主鍵關聯,在這種關聯關係中,要求兩個對象的主鍵必須保持一致,經過兩個表的主鍵創建關聯關係須外鍵參與。 無法傳圖 我就用這個表示吧 看idjava

 Copany         Address  數據庫

 id<Pk>          id<Pk><Fk>app

 name              homeide

基於主鍵的映射策略:指一端的主鍵生成器使用 foreign 策略,代表根據」對方」的主鍵來生成本身的主鍵,本身並不能獨立生成主鍵. <param> 子元素指定使用當前持久化類的哪一個屬性做爲 「對方」測試

 

<generator class="foreign">
                <param name="property">idcharbean</param>
            </generator>

採用foreign主鍵生成器策略的一端增長 one-to-one 元素映射關聯屬性,其 one-to-one 屬性還應增長 constrained=「true」 屬性;另外一端(company)增長one-to-one元素映射關聯屬性。ui

constrained(約束):指定爲當前持久化類對應的數據庫表的主鍵添加一個外鍵約束,引用被關聯的對象(「對方」)所對應的數據庫表主鍵this

    <one-to-one name="idcharbean" class="Idcharbean" constrained="true"></one-to-one>
   

無有外鍵方(Idcharbean)配置 bean:spa

  

package com.java.bean;

public class Idcharbean  {
    private int id;
    private int nameber;
    private Person person;

    public int getId() {
        return id;
    }

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

    public int getNameber() {
        return nameber;
    }

    public void setNameber(int nameber) {
        this.nameber = nameber;
    }

    public Person getPerson() {
        return person;
    }

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

hm.xml 配置 hibernate

<?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.java.bean">
    <class name="Idcharbean" table="t_idcahr">
        <id name="id" type="int" column="id">
            <generator class="identity"/> <!--這個地方的配置是一對一的關鍵喲-->
        </id>
        <property name="nameber" type="int" column="nameber" length="20"/>
        <one-to-one name="person" class="Person"></one-to-one>
    </class>
</hibernate-mapping>

有外鍵方(Person)配置 beancode

  

package com.java.bean;


public class Person  {
    private int id;
    private String name;
    private Idcharbean idcharbean;

    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 Idcharbean getIdcharbean() {
        return idcharbean;
    }

    public void setIdcharbean(Idcharbean idcharbean) {
        this.idcharbean = idcharbean;
    }
}

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.java.bean">
    <class name="Person" table="t_person">
        <id name="id" type="int" column="id">
            <generator class="foreign">
                <param name="property">idcharbean</param> <!--這個就是共同主鍵-->
            </generator>
        </id>
        <property name="name" type="string" column="name" length="20"/>
        <one-to-one name="idcharbean" class="Idcharbean" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>

關於 foregin:表示主鍵策略使用foreign

 *表示根據對方主鍵生成本身的主鍵

*param子元素指定使用前持久化

關於 constrained="true"
*指定爲當前持久化類的對應表的主鍵添加外鍵約束 應用被關聯的對象 "對方"所對應的數據庫表主鍵

配置完成後寫個增長 玩一下
  
public class Idchardao {
    private static SessionFactory sf;
    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sf = configuration.buildSessionFactory(serviceRegistry);
        }catch (Exception e){
            throw e;
        }
    }
    public void add(Idcharbean idcharbean){
        Session s = sf.openSession();
        Transaction t =null;
        try {
            t = s.beginTransaction();
            s.save(idcharbean);
            t.commit();
        }catch (Exception e){
            t.rollback();
        }finally {
            s.close();
        }
    }
}

 ps須要兩個類dao裏都有add喲 

 而後是test文件 添加一下

@Test
public void testAdd() throws Exception {
    Idcharbean idcharbean = new Idcharbean();
        idcharbean.setNameber(1111);
    Person person = new Person();
        person.setName("馬虎");

        idcharbean.setPerson(person);
        person.setIdcharbean(idcharbean);

    new Idchardao().add(idcharbean);
    new Psersondao().add(person);


}

而後再寫個  獲取id能夠獲取到關聯的對方id的小測試文件

  

public Idcharbean Get(){
Session s = sf.openSession();
Transaction t = null;
Idcharbean idcharbean = null;
try {
t = s.beginTransaction();
idcharbean = (Idcharbean) s.get(Idcharbean.class,1);
t.commit();
}catch (Exception e){
t.rollback();
}finally {
s.close();
}
return idcharbean;
}

 不如在搞個 移除關係 說動手就動手 

  

  //解除關係
    public void Remove() {
        Session s = sf.openSession();
        Transaction t = null;
        try {
            t = s.beginTransaction();
            Person person = (Person) s.get(Person.class, 1);
            person.setIdcharbean(null);
            t.commit();
        } catch (Exception e) {
            t.rollback();
        } finally {
            s.close();
        }

    }       

 既然都移除了不如再來個刪除玩玩 

  

          //刪除
        public void delet(){
                Session s = sf.openSession();
                Transaction t = null;
                try {
                    t = s.beginTransaction();
                    Person person = (Person) s.get(Person.class,1);

                        s.delete(person);
                    /* Idcharbean idcharbean = (Idcharbean) s.get(Idcharbean.class,1);
                        s.delete(idcharbean);*/
                        t.commit();
                }catch (Exception e){
                    t.rollback();
                }finally {
                    s.close();
                }
    }
喲 寫完了 更多的方法 我在多對多中搞吧= = 話說爲啥 我多對多還沒寫就寫一對一了 我是否是瘋了
相關文章
相關標籤/搜索