2009年11月27日 #
如果value等于if1時(shí),DECODE函數(shù)的結(jié)果返回then1,....,如何不等于任何一個(gè)if值,則返回else
2.sign(變量1-變量2)
如果(變量1-變量2)大于0返回1,小于0返回-1,等于0返回0
A = dao.read(id);
List<B> bList = A.getBList();
bList.clear();
B b1 = new B();
b1.setA(A);
bList.add(b1);
B b2 = new B();
b2.setA(A);
bList.add(b2);
A.
比如我們現(xiàn)在有一個(gè)Parent的實(shí)體,在Parent實(shí)體當(dāng)中有一個(gè)children的Set
由于這個(gè)children的數(shù)據(jù)并非非常重要,為了方便,我們?cè)谛薷膒arent的時(shí)候,做法經(jīng)常都是
1,清空children全部刪了,再把新的children全部加進(jìn)去。
今天早上做這一方面工作的時(shí)候遇到一個(gè)問(wèn)題,就是在更新parent的時(shí)候,報(bào)了一個(gè)
Don't dereference a collection with cascade="all-delete-orphan"
的異常,經(jīng)常一半個(gè)小時(shí)的查資料和調(diào)試,終于找到了解決問(wèn)題的辦法
剛剛開(kāi)始我的做法為:
parent = parentService.findParentById(id);
parent.getChildren.clear();
parent.setChildren(newChildren);
parentService.updateparent(parent);
這樣做一定會(huì)報(bào)出一個(gè)Don't dereference a collection with cascade="all-delete-orphan"的異常
原來(lái)是,對(duì)于parent的children這個(gè)Set,它本身是一個(gè)持久的集合,該集合存在于hibernate的對(duì)象池當(dāng)中,通過(guò)
parent.setChildren(newChildren)的設(shè)置之后,本身已經(jīng)將parent對(duì)children集合的引用指到對(duì)象池外的一個(gè)集合。
后來(lái)查詢(xún)資料后的做法為:
parent = parentService.findParentById(id);
parent.getChildren.clear();
parent.getChildren.addAll(newChildren);
parentService.updateparent(parent);
做了幾次測(cè)試,問(wèn)題解決。
hbm配置如下:
<set lazy="true" name="children" cascade="all,delete-orphan" inverse="true">
<key column="PARENT_ID"/>
<one-to-many class="Child"/>
</set>
另外,hibernate 3已經(jīng)將cascade當(dāng)中的選項(xiàng)做了修改,現(xiàn)在已經(jīng)沒(méi)有了all-delete-orphan這個(gè)選項(xiàng),雖然hibernate
內(nèi)部還是支持這個(gè)選項(xiàng)……
mvn test -Dtest=myTest //運(yùn)行某一個(gè)單元測(cè)試類(lèi)
在數(shù)據(jù)庫(kù)表中會(huì)有這樣的一個(gè)字段用來(lái)區(qū)別記錄的屬性,如:在客戶(hù)表中有一個(gè)字段表示客戶(hù)級(jí)別,當(dāng)這個(gè)記錄為A時(shí)是一級(jí)客戶(hù),為B時(shí)是二級(jí)客戶(hù)。在用hiberante做OR表示時(shí)類(lèi)可能是這樣的:
public class Customer{
private String flag; //表示客戶(hù)的級(jí)別
...
}
然后,在程序中手動(dòng)控制flag的值,但是這樣當(dāng)每個(gè)級(jí)的客戶(hù)有不同的屬性時(shí)Customer類(lèi)將包含所有級(jí)別的屬性,這樣不是很好。
hibernate提供一個(gè)Discriminator映射的方法,就是把一個(gè)表映射成不同的類(lèi),有不同的屬性。
public class Customer{
//包含所有級(jí)別的公共屬性
...
}
public class CustomerA extends Customer{
//只包括一級(jí)客戶(hù)的特有屬性
}
public class CustomerB extends Customer{
//只包含二級(jí)客戶(hù)特有的屬性
}
這樣更符合面向?qū)ο蟮脑瓌t,然后在hbm.xml中這樣寫(xiě):
<id name="id" type="int">
...
</id>
<discriminator column="flag" type="string" />
<!-- 公共屬性的映射 -->
<subclass name="CustomerA" discriminator-value="A">
<!-- 一級(jí)客戶(hù)特有屬性的映射 -->
</subclass>
<subclass name="CustomerB" discriminator-value="B">
<!-- 二級(jí)客戶(hù)特有屬性的映射 -->
</subclass>
這樣就可以單獨(dú)的用CustomerA,CustomerB這樣的實(shí)例了,做數(shù)據(jù)庫(kù)修改時(shí)就不用關(guān)心flag字段的值了,會(huì)自動(dòng)的加A或B。
如果是使用hibernate Annotation而不是xml來(lái)描述映謝關(guān)系,代碼如下:
@Entity
@Table(name = "customer")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "flag", discriminatorType = DiscriminatorType.STRING)
public class Customer{
}
@Entity
@DiscriminatorValue(value = "A")
public class CustomerA extends Customer{
}
@Entity
@DiscriminatorValue(value = "B")
public class CustomerB extends Customer{
}
這樣就可以了。
--簡(jiǎn)單Case函數(shù)
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函數(shù)
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END
這兩種方式,可以實(shí)現(xiàn)相同的功能。簡(jiǎn)單Case函數(shù)的寫(xiě)法相對(duì)比較簡(jiǎn)潔,但是和Case搜索函數(shù)相比,功能方面會(huì)有些限制,比如寫(xiě)判斷式。
還有一個(gè)需要注意的問(wèn)題,Case函數(shù)只返回第一個(gè)符合條件的值,剩下的Case部分將會(huì)被自動(dòng)忽略。
--比如說(shuō),下面這段SQL,你永遠(yuǎn)無(wú)法得到“第二類(lèi)”這個(gè)結(jié)果
CASE WHEN col_1 IN ( 'a', 'b') THEN '第一類(lèi)'
WHEN col_1 IN ('a') THEN '第二類(lèi)'
ELSE'其他' END
下面我們來(lái)看一下,使用Case函數(shù)都能做些什么事情。
一,已知數(shù)據(jù)按照另外一種方式進(jìn)行分組,分析。
有如下數(shù)據(jù):(為了看得更清楚,我并沒(méi)有使用國(guó)家代碼,而是直接用國(guó)家名作為Primary Key)
國(guó)家(country) 人口(population)
中國(guó) 600
美國(guó) 100
加拿大 100
英國(guó) 200
法國(guó) 300
日本 250
德國(guó) 200
墨西哥 50
印度 250
根據(jù)這個(gè)國(guó)家人口數(shù)據(jù),統(tǒng)計(jì)亞洲和北美洲的人口數(shù)量。應(yīng)該得到下面這個(gè)結(jié)果。
洲 人口
亞洲 1100
北美洲 250
其他 700
想要解決這個(gè)問(wèn)題,你會(huì)怎么做?生成一個(gè)帶有洲Code的View,是一個(gè)解決方法,但是這樣很難動(dòng)態(tài)的改變統(tǒng)計(jì)的方式。
如果使用Case函數(shù),SQL代碼如下:
SELECT SUM(population),
CASE country
WHEN '中國(guó)' THEN '亞洲'
WHEN '印度' THEN '亞洲'
WHEN '日本' THEN '亞洲'
WHEN '美國(guó)' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END
FROM Table_A
GROUP BY CASE country
WHEN '中國(guó)' THEN '亞洲'
WHEN '印度' THEN '亞洲'
WHEN '日本' THEN '亞洲'
WHEN '美國(guó)' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END;
同樣的,我們也可以用這個(gè)方法來(lái)判斷工資的等級(jí),并統(tǒng)計(jì)每一等級(jí)的人數(shù)。SQL代碼如下;
SELECT
CASE WHEN salary <= 500 THEN '1'
WHEN salary > 500 AND salary <= 600 THEN '2'
WHEN salary > 600 AND salary <= 800 THEN '3'
WHEN salary > 800 AND salary <= 1000 THEN '4'
ELSE NULL END salary_class,
COUNT(*)
FROM Table_A
GROUP BY
CASE WHEN salary <= 500 THEN '1'
WHEN salary > 500 AND salary <= 600 THEN '2'
WHEN salary > 600 AND salary <= 800 THEN '3'
WHEN salary > 800 AND salary <= 1000 THEN '4'
ELSE NULL END;
二,用一個(gè)SQL語(yǔ)句完成不同條件的分組。
有如下數(shù)據(jù)
國(guó)家(country) 性別(sex) 人口(population)
中國(guó) 1 340
中國(guó) 2 260
美國(guó) 1 45
美國(guó) 2 55
加拿大 1 51
加拿大 2 49
英國(guó) 1 40
英國(guó) 2 60
按照國(guó)家和性別進(jìn)行分組,得出結(jié)果如下
國(guó)家 男 女
中國(guó) 340 260
美國(guó) 45 55
加拿大 51 49
英國(guó) 40 60
普通情況下,用UNION也可以實(shí)現(xiàn)用一條語(yǔ)句進(jìn)行查詢(xún)。但是那樣增加消耗(兩個(gè)Select部分),而且SQL語(yǔ)句會(huì)比較長(zhǎng)。
下面是一個(gè)是用Case函數(shù)來(lái)完成這個(gè)功能的例子
SELECT country,
SUM( CASE WHEN sex = '1' THEN
population ELSE 0 END), --男性人口
SUM( CASE WHEN sex = '2' THEN
population ELSE 0 END) --女性人口
FROM Table_A
GROUP BY country;
這樣我們使用Select,完成對(duì)二維表的輸出形式,充分顯示了Case函數(shù)的強(qiáng)大。
三,在Check中使用Case函數(shù)。
在Check中使用Case函數(shù)在很多情況下都是非常不錯(cuò)的解決方法。可能有很多人根本就不用Check,那么我建議你在看過(guò)下面的例子之后也嘗試一下在SQL中使用Check。
下面我們來(lái)舉個(gè)例子
公司A,這個(gè)公司有個(gè)規(guī)定,女職員的工資必須高于1000塊。如果用Check和Case來(lái)表現(xiàn)的話,如下所示
CONSTRAINT check_salary CHECK
( CASE WHEN sex = '2'
THEN CASE WHEN salary > 1000
THEN 1 ELSE 0 END
ELSE 1 END = 1 )
如果單純使用Check,如下所示
CONSTRAINT check_salary CHECK
( sex = '2' AND salary > 1000 )
女職員的條件倒是符合了,男職員就無(wú)法輸入了。
update (ctarticle as a left join ctclass as c on a.classid = c.classid) left join cttag as b on a.articleid = b.articleid
set tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
where a.classid=23 and a.nclassid=0 and tagid is not null
update (ctarticle as a left join (ctnclass as c left join ctclass as d on c.classid = d.classid) on a.nclassid = c.nclassid and a.classid = c.classid) left join cttag as b on a.articleid = b.articleid set tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid where a.classid=23 and a.nclassid=197;
更新操作
左連接中數(shù)據(jù)的篩選
insert into cttag(articleid,classid,nclassid) select a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null
//本語(yǔ)句功能為, 顯示主表的全部?jī)?nèi)容, 插入數(shù)據(jù)到副表中沒(méi)有的數(shù)據(jù)
//主要作用為: 讓數(shù)據(jù)減少冗余
上例中的延續(xù)
select a.*, b.*, c.*, d.*
from cttag as d left join ((ctarticle as a left join ctclass as b on a.classid=b.classid) left join ctnclass as c on a.nclassid=c.nclassid) on d.articleid=a.articleid;
顯示文章表中的全部, 調(diào)用類(lèi)別表中的欄目
select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid
//作用, 有時(shí)在文章表中包含了在個(gè)別類(lèi)別表中沒(méi)有的數(shù)據(jù), 用這個(gè)語(yǔ)法可以讀出文章表的全部數(shù)據(jù)
//a 為 文章表, b 為主類(lèi)別, c 為子類(lèi)別
同上例, 選擇追加數(shù)據(jù)時(shí)加上空格
insert into cttag(articleid,classid,nclassid,tag)
select a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
from (ctarticle as a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) left join cttag as b on a.articleid = b.articleid where a.classid=4 and a.nclassid=154;
連接n個(gè)表, 并追加數(shù)據(jù)到其中一個(gè)表, n=4
insert into cttag(articleid,classid,nclassid,tag)
select a.articleid,a.classid,a.nclassid,d.class+c.nclass
from (ctarticle as a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) left join cttag as b on a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
//解讀
插入到 表2(欄1,欄2,欄3,欄4)
選擇 別名a.欄1, 別名a.欄2, 別名a.欄3, 別名d.欄4 加上 別名c.欄5
從 (表1 別名a 左連接 (表3 別名c 左連接 表4 別名d 在 別名c.欄2 等于 別名d.欄2) 在 別名a.欄2 等于 別名c.欄2 和 別名a.欄3=別名c.欄3) 左連接 表2 別名b 在 別名a.欄1 等于 別名b.欄1 在那里 別名a.欄2=1 和 別名a.欄3=1
連接兩個(gè)表, 并追加數(shù)據(jù)到其中一個(gè)表
insert into cttag(articleid,classid,nclassid)
select a.articleid,a.classid,a.nclassid
from ctarticle as a left join cttag as b on a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
//解讀
插入到 表2(欄1,欄2,欄3)
選擇 別名a.欄1, 別名a.欄2, 別名a.欄3
從 表1 別名a 左連接 表2 別名b 在 別名a.欄1 等于 別名b.欄1 在那里 別名a.欄4=1 和 別名a.欄5=1
左連接
同步兩表的數(shù)據(jù)
update ctarticle a inner join cttag b on a.articleid = b.articleid set b.classid=a.classid, b.nclassid=a.nclassid;
//解讀
更新 表1 別名a 聯(lián)接 表2 別名2 在 別名a.欄1 等于 別名b.欄1 設(shè)置 別名b.欄2 更新為 別名a.欄2, 別名b.欄3 更新為 別名a.欄3
右外連接
select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20
查詢(xún)別名 a,b 表, 只匹配 b 表中的內(nèi)容.
添加數(shù)據(jù)到連接表之一
insert into cttag ( tag, articleid ) select top 1 b.tag, a.articleid from ctarticle as a left join cttag as b on a.articleid = b.articleid where a.articleid order by a.articleid desc;
變通中的用法二
insert into bureply
select b.*, a.classid, a.nclassid
from article as a inner join reply as b on a.articleid = b.articleid
where classid=50;
實(shí)際應(yīng)用中的變通
insert into butag ( tag, articleid, classid, nclassid)
select b.tag, a.articleid, a.classid, a.nclassid
from article as a inner join tag as b on a.articleid = b.articleid
where classid=24;
添加數(shù)據(jù)到其他表
insert into butag ( tag, articleid )
select b.tag, a.articleid
from article as a inner join tag as b on a.articleid = b.articleid
where a.articleid<>false;
//解讀
添加到 接收表(列1,列2)
選擇 別名b.列1, 別名a.列2
從 表1 表名a 聯(lián)接 表2 表名b 在 別名a.列c 等于 別名b.列c
在哪里 別名a.列c 不等于 沒(méi)有
實(shí)際應(yīng)用中的變通
select b.tag, a.articleid, a.classid, a.nclassid
from article as a inner join tag as b on a.articleid = b.articleid
where a.classid=24;
查詢(xún)
select b.tag, a.articleid
from article as a inner join tag as b on a.articleid = b.articleid
where a.articleid<>false;
//解讀
選擇 別名b.列, 別名a.列
從 表1 別名a 聯(lián)接 表2 別名b 在 別名a.列c = 別名b.列c
在哪里 別名a.列c 不等于 沒(méi)有
注: as 不是必要
IN:確定給定的值是否與子查詢(xún)或列表中的值相匹配。
IN 關(guān)鍵字使您得以選擇與列表中的任意一個(gè)值匹配的行。
當(dāng)要獲得居住在 California、Indiana 或 Maryland 州的所有作者的姓名和州的列表時(shí),就需要下列查詢(xún):
SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID = 1 OR CategoryID = 4 OR CategoryID = 5
然而,如果使用 IN,少鍵入一些字符也可以得到同樣的結(jié)果:
SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID IN (1, 4, 5)
IN 關(guān)鍵字之后的項(xiàng)目必須用逗號(hào)隔開(kāi),并且括在括號(hào)中。
下列查詢(xún)?cè)?titleauthor 表中查找在任一種書(shū)中得到的版稅少于 50% 的所有作者的 au_id,然后從 authors 表中選擇 au_id 與
titleauthor 查詢(xún)結(jié)果匹配的所有作者的姓名:
SELECT au_lname, au_fname FROM authors WHERE au_id IN (SELECT au_id FROM titleauthor WHERE royaltyper < 50)
結(jié)果顯示有一些作者屬于少于 50% 的一類(lèi)。
NOT IN:通過(guò) NOT IN 關(guān)鍵字引入的子查詢(xún)也返回一列零值或更多值。
以下查詢(xún)查找沒(méi)有出版過(guò)商業(yè)書(shū)籍的出版商的名稱(chēng)。
SELECT pub_name FROM publishers WHERE pub_id NOT IN (SELECT pub_id FROM titles WHERE type = 'business')
使用 EXISTS 和 NOT EXISTS 引入的子查詢(xún)可用于兩種集合原理的操作:交集與差集。兩個(gè)集合的交集包含同時(shí)屬于兩個(gè)原集合的所有元素。
差集包含只屬于兩個(gè)集合中的第一個(gè)集合的元素。
EXISTS:指定一個(gè)子查詢(xún),檢測(cè)行的存在。
本示例所示查詢(xún)查找由位于以字母 B 開(kāi)頭的城市中的任一出版商出版的書(shū)名:
SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type =
'business')
SELECT distinct pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE type = 'business')
兩者的區(qū)別:
EXISTS:后面可以是整句的查詢(xún)語(yǔ)句如:SELECT * FROM titles
IN:后面只能是對(duì)單列:SELECT pub_id FROM titles
NOT EXISTS:
例如,要查找不出版商業(yè)書(shū)籍的出版商的名稱(chēng):
SELECT pub_name FROM publishers WHERE NOT EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type =
'business')
下面的查詢(xún)查找已經(jīng)不銷(xiāo)售的書(shū)的名稱(chēng):
SELECT title FROM titles WHERE NOT EXISTS (SELECT title_id FROM sales WHERE title_id = titles.title_id)
在數(shù)據(jù)庫(kù)表中會(huì)有這樣的一個(gè)字段用來(lái)區(qū)別記錄的屬性,如:在客戶(hù)表中有一個(gè)字段表示客戶(hù)級(jí)別,當(dāng)這個(gè)記錄為A時(shí)是一級(jí)客戶(hù),為B時(shí)是二級(jí)客戶(hù)。在用hiberante做OR表示時(shí)類(lèi)可能是這樣的:
public class Customer{
private String flag; //表示客戶(hù)的級(jí)別
...
}
然后,在程序中手動(dòng)控制flag的值,但是這樣當(dāng)每個(gè)級(jí)的客戶(hù)有不同的屬性時(shí)Customer類(lèi)將包含所有級(jí)別的屬性,這樣不是很好。
hibernate提供一個(gè)Discriminator映射的方法,就是把一個(gè)表映射成不同的類(lèi),有不同的屬性。
public class Customer{
//包含所有級(jí)別的公共屬性
...
}
public class CustomerA extends Customer{
//只包括一級(jí)客戶(hù)的特有屬性
}
public class CustomerB extends Customer{
//只包含二級(jí)客戶(hù)特有的屬性
}
這樣更符合面向?qū)ο蟮脑瓌t,然后在hbm.xml中這樣寫(xiě):
<id name="id" type="int">
...
</id>
<discriminator column="flag" type="string" />
<!-- 公共屬性的映射 -->
<subclass name="CustomerA" discriminator-value="A">
<!-- 一級(jí)客戶(hù)特有屬性的映射 -->
</subclass>
<subclass name="CustomerB" discriminator-value="B">
<!-- 二級(jí)客戶(hù)特有屬性的映射 -->
</subclass>
這樣就可以單獨(dú)的用CustomerA,CustomerB這樣的實(shí)例了,做數(shù)據(jù)庫(kù)修改時(shí)就不用關(guān)心flag字段的值了,會(huì)自動(dòng)的加A或B。
如果是使用hibernate Annotation而不是xml來(lái)描述映謝關(guān)系,代碼如下:
@Entity
@Table(name = "customer")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "flag", discriminatorType = DiscriminatorType.STRING)
public class Customer{
}
@Entity
@DiscriminatorValue(value = "A")
public class CustomerA extends Customer{
}
@Entity
@DiscriminatorValue(value = "B")
public class CustomerB extends Customer{
}
這樣就可以了。
今天在做項(xiàng)目的時(shí)候遇到一個(gè)問(wèn)題,同一個(gè)action里使用不同的操作(增刪改查)的時(shí)候總是獲得當(dāng)前系統(tǒng)時(shí)間是同一個(gè)時(shí)間,后來(lái)檢查了下才知道是spring.xml里的action映射里沒(méi)有加scope="prototype"屬性 scope="prototype"沒(méi)寫(xiě)的問(wèn)題,項(xiàng)目中對(duì)一個(gè)表的增刪該操作是用一個(gè)action,這個(gè)action有add,update,delete,save這些方法,添加和修改是共用一個(gè)頁(yè)面,當(dāng)頁(yè)面得到id時(shí)代表進(jìn)行的修改操作,反之是添加操作。因?yàn)樵谂渲胹pring的bean是忘了寫(xiě)scope="prototype"所以每次添加時(shí)都顯示最后一次訪問(wèn)過(guò)的記錄,找了很長(zhǎng)時(shí)間,原來(lái)是spring bean出了問(wèn)題。 scope="prototype" 會(huì)在該類(lèi)型的對(duì)象被請(qǐng)求時(shí)創(chuàng)建一個(gè)新的action對(duì)象。如果沒(méi)有配置scope=prototype則添加的時(shí)候不會(huì)新建一個(gè)action,他任然會(huì)保留上次訪問(wèn)的過(guò)記錄的信息