JavaWeb?Hibernate使用全面介紹
前言
以下內(nèi)容科班同學(xué)學(xué)過UML和數(shù)據(jù)庫的應(yīng)該比較熟悉
數(shù)據(jù)模型:數(shù)據(jù)模型是對數(shù)據(jù)庫特征的抽象,也就是用戶從數(shù)據(jù)庫中看到的模型,例如一張數(shù)據(jù)表或者用戶從數(shù)據(jù)表中所看到的存儲信息,此模型既要面向?qū)ο笥忠嫦蛳到y(tǒng)
領(lǐng)域模型:領(lǐng)域模型是對現(xiàn)實世界中的對象的可視化表現(xiàn),又稱為概念模型,領(lǐng)域模型或者分析對象模式,沒有所謂唯一正確的領(lǐng)域模型,所有模型都是對我們試圖要理解的領(lǐng)域的近似,領(lǐng)域模型主要是在特定群體用于理解和溝通的工具
一、實體關(guān)聯(lián)之間的映射
接下來的內(nèi)容與數(shù)據(jù)庫中的ER圖映射成關(guān)系表十分相似
1、單向多對一的關(guān)聯(lián)
兩個類映射成表,并且把被關(guān)聯(lián)表的主鍵作為關(guān)聯(lián)表的外鍵
<hibernate-mapping> <class name="com.mr.product.Product" table="tab_product"> <id name="id" column="id" type="int"> <generator class="native"/> </id> <property name="name" type="string" length="45"> <column name="name"/> </property> <property name="price" type="double"> <column name="price"/> </property> <many-to-one name="factory" class="com.mr.factory.Factory"> <column name="factoryid"/> </many-to-one> </class> </hibernate-mapping>
2、多對一雙向關(guān)聯(lián)
以任一個表的主鍵作另一個表的外鍵都可以
<hibernate-mapping> <class name="com.mr.product.Product" table="tab_product"> <id name="id" column="id" type="int"> <generator class="native"/> </id> <property name="name" type="string" length="45"> <column name="name"/> </property> <set name="products" inverse="true"> <key column="factoryid"/> </property> <one-to-many class="com.mr.product.Product"> </set> </class> </hibernate-mapping>
3、一對一主鍵關(guān)聯(lián)
指的是兩個表之間通過主鍵形成一對一的映射
<hibernate-mapping> <class name="com.mr.people.People" table="tab_people"> <id name="id" column="id" type="int"> <generator class="native"/> </id> <property name="name" type="string" length="45"> <column name="name"/> </property> <property name="sex" type="string" length="2"> <column name="sex"/> </property> <property name="age" type="int"> <column name="age"/> </property> <ont-to-ont name="com.mr.idcard.IDcard" cascade="all"/> </class> </hibernate-mapping>
4、一對一外鍵關(guān)聯(lián)
這個配置比較簡單 添加一個新字段即可 此處不再贅述
5、多對多關(guān)聯(lián)
多對多關(guān)聯(lián)比較特殊 需要另外的一張表來保存多對多的映射關(guān)系
<hibernate-mapping> <class name="com.mr.user.User" table="tab_user"> <id name="id"> <generator class="native"/> </id> <property name="name" not-null="true"/> <set name="roles" table="tab_mapping"> <key column=""user_id"></key> <many-to-many class="com.mr.role.Role" column="role_id"/> </set> </class> </hibernate-mapping>
6、級聯(lián)操作
級聯(lián)操作指的是當(dāng)主控方執(zhí)行對數(shù)據(jù)表的更改時,關(guān)聯(lián)對象是否進行同步操作,在映射文件中通過對cascade屬性的設(shè)置決定是否對關(guān)聯(lián)對象采用級聯(lián)操作
all 所有情況下均采用級聯(lián)操作
none 默認參數(shù) 所有情況下均不采用級聯(lián)操作
save-update 在執(zhí)行save-update方法時執(zhí)行級聯(lián)操作
delete 在執(zhí)行delete方法時執(zhí)行級聯(lián)操作
二、實體繼承關(guān)系映射
繼承是面向?qū)ο蟮闹匾匦?在Hibernate中是以面向?qū)ο蟮乃枷脒M行持久化操作的,所以在Hibernate中數(shù)據(jù)表所映射的實體的對象也是可以存在繼承關(guān)系的 主要有以下三種
1、類繼承樹映射成一張表
學(xué)生和職員共同繼承了人的實體對象,也將擁有人的實體的對象的全部屬性 可以將這三個類映射到一張表中 添加一個字段來區(qū)分同名屬性
<hibernate-mapping package="com.mr.person"> <class name="Person" table="tab_person"> <id name="id"> <generator class="native"/> </id> <discriminator column="type" type="string"/> <property name="name" not-null="true"/> <property name="age" type="int"/> perperty name="sex" type="string"/> <subclass name="Studeng" discrimination-value="學(xué)生"> <property name="school"/> </subclass> <subclass name="Staffer" discrimination-value="職員"> <property name="company"/> </subclass> </class> </hibernate-mapping>
2、每個子類映射成一張表
上述的三個類也可以將每個子類映射成一張表 兩個子類映射的表都將通過主鍵關(guān)聯(lián)到超類映射的表,形成一對一的關(guān)系
<hibernate-mapping package="com.mr.person"> <class name="Person" table="tab_person"> <id name="id"> <generator class="native"/> </id> <discriminator column="type" type="string"/> <property name="name" not-null="true"/> <property name="age" type="int"/> perperty name="sex" type="string"/> <joined-subclass name="Student" table="tab_student"> <key column="id"/> <property name="school"/> </joined-subclass> <joined-subclass name="Staffer" discrimination-value="職員"> <key column="id"/> </joined-subclass> </class> </hibernate-mapping>
3、每個具體類映射成一張表
將 每個具體類映射成一張表 每個子類的映射表中都含有繼承的父類屬性映射的字段
<hibernate-mapping package="com.mr.person"> <class name="Person" abstract="true"> <id name="id"> <generator class="assigned"/> </id> <discriminator column="type" type="string"/> <property name="name" not-null="true"/> <property name="age" type="int"/> perperty name="sex" type="string"/> <union-subclass name="Student" table="tab_student"> <key column="id"/> <property name="school"/> </union-subclass> <union-subclass name="Staffer" discrimination-value="職員"> <key column="id"/> </union-subclass> </class> </hibernate-mapping>
三、Hibernate查詢語言
HQL(Hibernate Query Language)是完全面向?qū)ο蟮牟樵冋Z言,它提供了更加面向?qū)ο蟮姆庋b,它可以理解如多態(tài), 繼承和關(guān)聯(lián)的概念 HQL看上去與SQL相似 但它卻提供了更加強大的查詢功能
HQL是面向?qū)ο蟮牟樵冋Z言,所以它需要從目標(biāo)對象中查詢信息并返回匹配單個實體或多個實體對象的集合,而SQL語句是從數(shù)據(jù)庫中查找指定信息,返回的單條信息或多條信息的集合
所以HQL區(qū)分大小寫 語法如下
select "對象.屬性名"
from "對象"
where "條件"
group by"對象.屬性名" having "分組條件"
order by "對象.屬性名"
實體對象查詢
from person
最好指定 一個別名 方便其他地方引用
from person per
此時返回的是person對象中的所有數(shù)據(jù)
查詢指定字段數(shù)據(jù)
select Person(id,name) from Person per
HQL參數(shù)綁定機制
參數(shù)綁定機制可以使查詢語句和參數(shù)具體值相互獨立,不但可以提高程序開發(fā)效率,還可以有效的防止SQL的注入攻擊 HQL中有以下兩種方法
1:利用順序占位符?替代具體參數(shù)
session=HibernateUtil.getSession(); String sql="from Employee emp where emp.sex=?"; Query q=session.createQuery(hql); q.setParameter(0,"男"); emplist=q.list();
2:利用占位符:parameter 替代具體參數(shù)
session=HibernateUtil.getSession(); String sql="from Employee emp where emp.sex=:sex"; Query q=session.createQuery(hql); q.setParameter("sex","男"); emplist=q.list();
其余各種查詢與SQL基本一致 此處不再贅述
到此這篇關(guān)于JavaWeb Hibernate使用全面介紹的文章就介紹到這了,更多相關(guān)JavaWeb Hibernate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springcloud如何使用Feign后臺內(nèi)部傳遞MultipartFile
這篇文章主要介紹了springcloud如何使用Feign后臺內(nèi)部傳遞MultipartFile,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Mybatis反射核心類Reflector的實現(xiàn)
本文主要介紹了Mybatis反射核心類Reflector的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11