Hibernate中map的研究之:一對多及節點中的inverse的研究(一)

*************

City.java

************

package blog.hibernate.domain;

public class City {

    private int id;
    private String name;
    private String postcode;
    private Nation nation;

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public Nation getNation() {
        return nation;
    }

    public void setNation(Nation nation) {
        this.nation = nation;
    }

    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;
    }
}

*************

City.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="blog.hibernate.domain">
    <class name="City" table="city">
        <id name="id" column="CITY_ID">
            <generator class="native"/>
        </id>
        <property name="name" column="CITY_NAME" type="string" not-null="true"/>
        <property name="postcode" column="POST_CODE"/>
         
        <many-to-one name="nation" class="Nation" column="NATION_ID" not-null="true" />
    </class>
</hibernate-mapping>



*************

Nation.java

************

package blog.hibernate.domain;

import java.util.Map;

public class Nation {

    private int id;
    private String name;
    private Map<String, City> citys;

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public Map<String, City> getCitys() {
        return citys;
    }

    public void setCitys(Map<String, City> citys) {
        this.citys = citys;
    }
}



*************

Nation.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="blog.hibernate.domain">
    <class name="Nation" table="nation">
        <id name="id" column="NATION_ID">
            <generator class="native"></generator>
        </id>
        <property name="name" column="NATION_NAME" not-null="true"></property>
                
        <map name="citys" inverse="false">
            <key column="NATION_ID" />
            <map-key column="CITYNAME" type="string" />
            <one-to-many class="City" />
        </map>
    </class> 
</hibernate-mapping>



*************

hibernate.cfg.xml

************

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property><!-- ///表示連接本機的數據庫//localhost:3306 -->
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">1234</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   		
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.show_sql">true</property>
        
        <mapping resource="blog/hibernate/domain/Nation.hbm.xml"/>
        <mapping resource="blog/hibernate/domain/City.hbm.xml"/>

    </session-factory>	
</hibernate-configuration>


*************

HibernateUtil.java

************

package blog.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public final class HibernateUtil {
	
	private static SessionFactory sessionFactory;
	private HibernateUtil(){}
	
	static{
		Configuration cfg = new Configuration();
		sessionFactory =  cfg.configure("hibernate.cfg.xml").buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}



*************

junit test

************

package juint.test;

import blog.hibernate.domain.Nation;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.BeforeClass;
import org.junit.Test;

import blog.hibernate.HibernateUtil;
import blog.hibernate.domain.City;
import java.util.HashMap;
import java.util.Map;

public class Many2OneAndOne2Many {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @Test
    public void test() {
        Map_Add();
    }


    public void Map_Add() {
        Session session = null;
        Transaction tx = null;
        try {

            City city1 = new City();
            city1.setName("中國·唐山");
            city1.setPostcode("063009");

            City city2 = new City();
            city2.setName("中國·天津");
            city2.setPostcode("356148");

            Map<String, City> citys = new HashMap<String, City>();
            citys.put("唐山", city1);
            citys.put("天津", city2);

            Nation nation = new Nation();
            nation.setName("中國");

            nation.setCitys(citys);
            //當Nation.hbm.xml文件中map節點的inverse = "false"或不寫時(即默認,false)
            //這行代碼的作用是Hibernate可以根據nation中的citys去更新city表中的CITYNAME
            //如果沒有setCitys則city表中的CITYNAME爲null
            //當Nation.hbm.xml文件中map節點的inverse = "true"時不起作用
            
            city1.setNation(nation);
            city2.setNation(nation);

            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            session.save(nation);
            session.save(city1);
            session.save(city2);
            tx.commit();
        } catch (Exception ex) {
            Logger.getLogger(Many2OneAndOne2Many.class.getName()).log(Level.SEVERE, null, ex);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}

---------------------------------------------------------------------------------------------------------------------------------------
注意:
City.hbm.xml中
屬性name 不爲空: <property name="name" column="CITY_NAME" type="string" not-null="true"/>
屬性nation 不爲空: <many-to-one name="nation" class="Nation" column="NATION_ID" not-null="true" />

Nation.hbm.xml中

屬性name 不爲空: <property name="name" column="NATION_NAME" not-null="true"></property>

---------------------------------------------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------------------------------------------

類Many2OneAndOne2Many中的Map_Add()方法中的代碼:nation.setCitys(citys);的作用:

            當Nation.hbm.xml文件中map節點的inverse = "false"或不寫時(即默認,false)
            這行代碼的作用是Hibernate可以根據nation中的citys去更新city表中的CITYNAME,也就是下面Hibernate會打印update語句的原因
            如果沒有setCitys則city表中的CITYNAME爲null,Hibernate不會去更新city表,也就不會打印update語句
            當Nation.hbm.xml文件中map節點的inverse = "true"時,這行代碼不起作用

---------------------------------------------------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------------------------------------------------

1、當屬性citys中 inverse 爲 false
<map name="citys" inverse="false">
            <key column="NATION_ID" />
            <map-key column="CITYNAME" type="string" />
            <one-to-many class="City" />

</map>


inverse = false

Hibernate打印出的sql語句爲:
Hibernate: insert into nation (NATION_NAME) values (?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?
Hibernate: update city set NATION_ID=?, CITYNAME=? where CITY_ID=?


數據庫內容爲:
+---------+------------+-----------+-----------+----------+
| CITY_ID | CITY_NAME  | POST_CODE | NATION_ID | CITYNAME |
+---------+------------+-----------+-----------+----------+
|       1 | 中國·唐山  | 063009    |         1 | 唐山     |
|       2 | 中國·天津  | 356148    |         1 | 天津     |
+---------+------------+-----------+-----------+----------+

---------------------------------------------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------------------------------------------

2、當屬性citys中 inverse 爲 true
<map name="citys" inverse="true">
            <key column="NATION_ID" />
            <map-key column="CITYNAME" type="string" />
            <one-to-many class="City" />

</map>

inverse = true

Hibernate: insert into nation (NATION_NAME) values (?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)
Hibernate: insert into city (CITY_NAME, POST_CODE, NATION_ID) values (?, ?, ?)

+---------+------------+-----------+-----------+----------+
| CITY_ID | CITY_NAME  | POST_CODE | NATION_ID | CITYNAME |
+---------+------------+-----------+-----------+----------+
|       1 | 中國·唐山  | 063009    |         1 | NULL     |
|       2 | 中國·天津  | 356148    |         1 | NULL     |
+---------+------------+-----------+-----------+----------+

而數據庫中的CITYNAME是通過nation.setCitys(citys);這行代碼注入值的。

---------------------------------------------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------------------------------------------

結論:

在操作兩個實例之間的鏈接時,如果沒有inverse屬性(也就是默認的inverse = false),hibernate會試圖執行兩個不同的sql語句,這兩者更新同一個外鍵列。通過指定

inverse = "true"時,顯式地告訴Hibernate鏈接的哪一端不應該與數據庫同步。這個例子中,告訴Hibernate它應該把在關聯的City端所做的變化傳播到數據庫,忽略只對citys集合所做的變化。


但是值得注意的是,inverse在有序的集合裏是不能使用的,這點要尤其注意。

---------------------------------------------------------------------------------------------------------------------------------------




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章