Create DataBase SHOPPING;
go
use SHOPPING;
go
/*==============================================================*/
/* Table: CATEGORIES */
/*==============================================================*/
create table CATEGORIES (
CATEGORY_ID bigint identity,
CATEGORY_NAME varchar(100) not null,
CATEGORY_DESCN varchar(500) null,
constraint PK_CATEGORIES primary key (CATEGORY_ID)
)
go
/*==============================================================*/
/* Table: PRODUCTS */
/*==============================================================*/
create table PRODUCTS (
PRODUCT_NO varchar(10) not null,
CATEGORY_ID bigint not null,
PRODUCT_NAME varchar(300) not null,
PRODUCT_PRICE float not null,
PHOTO_PATH varchar(100) null,
PRODUCT_DESCN varchar(2000) null,
constraint PK_PRODUCTS primary key (PRODUCT_NO)
)
go
/*==============================================================*/
/* Table: PRODUCT_SUPPLY */
/*==============================================================*/
create table PRODUCT_SUPPLY (
SUPPLY_NO varchar(10) null,
PRODUCT_NO varchar(10) null
)
go
/*==============================================================*/
/* Table: SUPPLIERS */
/*==============================================================*/
create table SUPPLIERS (
SUPPLY_NO varchar(10) not null,
SUPPLY_NAME varchar(200) not null,
SUPPLY_DESCN varchar(400) null,
constraint PK_SUPPLIERS primary key (SUPPLY_NO)
)
go
/*==============================================================*/
/* Create Relation */
/*==============================================================*/
alter table PRODUCTS
add constraint FK_PRODUCTS_REFERENCE_CATEGORI foreign key (CATEGORY_ID)
references CATEGORIES (CATEGORY_ID)
go
alter table PRODUCT_SUPPLY
add constraint FK_PRODUCT__REFERENCE_PRODUCTS foreign key (PRODUCT_NO)
references PRODUCTS (PRODUCT_NO)
go
alter table PRODUCT_SUPPLY
add constraint FK_PRODUCT__REFERENCE_SUPPLIER foreign key (SUPPLY_NO)
references SUPPLIERS (SUPPLY_NO)
go
創(chuàng)建數(shù)據(jù)庫(kù)腳本的
PRODUCTS(產(chǎn)品表) 和 CATEGORIES(類別表)一對(duì)多 PRODUCT_SUPPLY 為中間表 SUPPLIERS(供貨商表) 和 PRODUCTS 為多對(duì)多的關(guān)系。
products 表 hbm.xml
<many-to-one name="category" class="Category" cascade="save-update">
<column name="CATEGORY_ID" />
</many-to-one>
<!--多對(duì)多的關(guān)系中table指向的是中間表-->
<set name="supplys" table="PRODUCT_SUPPLY" cascade="save-update">
<!--key指向的是外鍵-->
<key column="PRODUCT_NO"></key>
<!--column對(duì)應(yīng)中間表中的外鍵-->
<many-to-many class="Supply" column="SUPPLY_NO"></many-to-many>
</set>
category 表 hbm.xml
<set name="productes" table="productes" cascade="save-update" inverse="true">
<!--key指向的是外鍵-->
<key column="CATEGORY_ID"></key>
<one-to-many class="Product"/>
</set>
supply 表 hbm.xml
<set name="products" table="PRODUCT_SUPPLY" inverse="true" cascade="save-update">
<key column="SUPPLY_NO"></key>
<many-to-many class="Product" column="PRODUCT_NO"></many-to-many>
</set>
1, 添加一個(gè)的新商品名稱為”Compaq 2620” 該商品屬于“筆記本”類別 由當(dāng)前所有的提供商提供貨源
List list = session.createQuery("from Supply").list(); Category c = (Category) session.get(Category.class, new Long(1)); product.setCategory(c); product.setSupplys(new HashSet(list)); session.save(product);
2, 查詢編號(hào)為” S0001”的提供商提供的所有商品 //通過(guò)隱式內(nèi)連接導(dǎo)航 List list = session.createQuery("from Product p where p.supplys.supply_no='S0001'").list(); 隱式內(nèi)連接導(dǎo)航 要注意的一個(gè)問(wèn)題是 從many端到 one 端 可以無(wú)限導(dǎo)航 但從one到many端只能導(dǎo)航一級(jí)
3,查詢編號(hào)為”S0002”的提供商提供的所有商品所涉及的類別 session.createQuery("from Category c where c.productes.product_no in (select p.product_no from Product p where p.supplys.supply_no='S0002' ) ").list(); 用到子查詢
4,查詢名稱為”TCL SHE8533”的商品的每個(gè)提供商的編號(hào)、名稱(部分屬性查詢) session.createQuery("select s.supply_no,s.supply_name from Supply s where s.products.product_name='TCL SHE8533'").list(); //投影查詢。如果想將查詢出來(lái)的 結(jié)果封裝成對(duì)象 用 select new package.Temp(s.a,s.b...) from .... Temp提供相應(yīng)的構(gòu)造方法包含可選的字段注意帶包名。
5, 查詢多于3種商品的類別信息(使用size函數(shù)處理) session.createQuery("from Category s where s.productes.size>3").list(); 注意其中的 size 表示的是 商品類別中產(chǎn)品數(shù)量多于3的類別。size用來(lái)處理集合中的大小
6,查詢至少有一個(gè)商品的類別信息(使用exists處理) session.createQuery("from Category c where exists( from c.productes) ").list();
7,查詢可以提供某種商品的供應(yīng)商信息(使用elements處理) session.createQuery("from Supply s where :product in elements(s.products) ")..setParameter("product", product).list(); product為 一個(gè) 對(duì)象 。 product in elements(s.products) 表示這個(gè)對(duì)象是否在這個(gè)集合中
8,使用本地SQL,顯示所有商品的商品名、價(jià)格以及類別信息,并降序排列。 session.createSQLQuery("select p.PRODUCT_NAME,p.PRODUCT_PRICE ,c.* from PRODUCTS p ,CATEGORIES c where p.CATEGORY_ID = c.CATEGORY_ID order by p.PRODUCT_PRICE desc") 的到的 集合中是個(gè) Object[]; 如果想返回對(duì)象 可以用 命名sql并在配置文件中指定返回的對(duì)象類型。
9 分頁(yè)查詢 :將商品按價(jià)格升序排列后,取第三頁(yè)的記錄,(每頁(yè)顯示2條記錄) Query query = session.createQuery("from Product p order by p.product_price ") .setFirstResult(2*(3-1)) .setMaxResults(2);
10,查詢所有類別的名字,及該類別包含的商品數(shù)量 (使用group by ,count函數(shù)) session.createQuery("select max(c.category_name), count(p) from Category c inner join c.productes p group by c.category_id ") 還有一種簡(jiǎn)單的方式就是 "select c.category_name, c.products.size from Category c "
11,批處理: 將某個(gè)類別下的商品修改為現(xiàn)有的另一個(gè)類別。 int count = session.createQuery("update Product p set p.category=:category where p.category.category_id='1'") .setParameter("category",c ).executeUpdate(); c為加載的一個(gè)新的類別
12,往數(shù)據(jù)庫(kù)中初始化3個(gè)名稱相同的商品(其他字段自行設(shè)置)。 要求:查詢所有商品,如果多個(gè)商品的名稱相同,則取其中任意一個(gè)完整的商品信息
hql = "from Prodcut pp where pp.product_no in (select max(p.category_id) from Product p group by p.product_name") ; 注意后面的一個(gè) 小技巧。 由于group by 后只能包含 group by字段和聚合函數(shù) 所以如果我想?yún)^(qū)別的字段似乎不可能 。但我們不妨將你要取的那個(gè)字段也加個(gè)聚合函數(shù)min 或 max 這樣就可以取出你要的 任意字段了。適應(yīng)sql sql2000中不妨 在pubs 下運(yùn)行 select max(title_id) as 編號(hào) ,count(type) as 數(shù)量, type from titles group by type 看看結(jié)果就知道了 雖然只 group by 了 type但 還是可以得到title_id
作者:caoyinghui1986 發(fā)表于2008-6-15 12:39:00
原文鏈接