Hibernate QBC的查詢方式的總結
(zhuan http://hi.baidu.com/liuchaoping66/blog/item/16a8858a575636d4fd1f10c7.html)
1
Criteria criteria = this.getSession().createCriteria(EquipmentPO.class);
2
criteria. add (Restrictions.allEq(new Map(Restrictions.eq("equipID",new String(" 11020449 ")),Restrictions.eq("equipID",new String(" 11020449 "))));
3
// 注意:between查詢條件可解釋為查詢EquipmentPO對象中的equipID屬性值在new String(" 11020449 ")和new String(" 11030137 ")之間的所有記錄值(包含兩個端點)
4
criteria. add (Restrictions. between ("equipID",new String(" 11020449 "),new String(" 11030137 ")));
5
// 查詢結果列表記錄按照equipID的屬性值來升序排序
6
criteria.addOrder( Order . asc ("equipID")); // 降序方式為: Order . desc ("equipID")
7
// 通過EquipmentPO對象的主鍵id來查詢
8
criteria. add (Restrictions.idEq("402882ac0d3f7ca8010d3f7ef869000b"));
9
Restrictions. like (fieldName, " % " + para + " % ")
10
// 使用ilike方式進行模糊查詢
11
criteria. add (Restrictions.ilike("equipID",new String(" % 11020 % ")));
12
// ilike的i即ignore之意,所以這里查詢出englishName值為"Optima XL 100K Ultracentrifuge"(忽略大小寫)的記錄
13
criteria. add (Restrictions.ilike("englishName",new String("Optima XL 100K Ultracentrifuge"),MatchMode.ANYWHERE)); // 這里
14
// 使用in方式有兩種形式,即數組或者Collection的方式,可參考如下兩個實例
15
// criteria. add (Restrictions. in ("equipID",new String [] {" 11020449 "," 11020450 "})); // 數組參數
16
Collection col = new ArrayList();
17
col. add (new String(" 11020449 "));
18
col. add (new String(" 11020450 "));
19
col. add (new String(" 11020874 "));
20
criteria. add (Restrictions. in ("equipID",col)); // Collection參數
21
// 使用isEmpty / isNotEmpty方式用來判斷EquipmentPO對象中的Collection類型的屬性是否為空的所有記錄
22
// EquipmentPO對象中定義屬性private Set equipFunctionDevelopPOs = new HashSet(); // 設備功能開發對象
23
criteria. add (Restrictions.isEmpty("equipFunctionDevelopPOs"));
24
criteria. add (Restrictions.isNotEmpty("equipFunctionDevelopPOs"));
25
// 使用isNull方式查詢出所有schoolID屬性沒有值的記錄。說明:Restrictions.isNull判斷屬性是否為空,為空返回true,反之返回false
26
criteria. add (Restrictions. isNull ("schoolID"));
27
criteria. add (Restrictions.isNotNull("schoolID"));
28
// 與Restrictions.eq正好相反,表示不存在( not in )
29
criteria. add (Restrictions. not (Restrictions.eq("equipID",new String(" 11020449 "))));
30
// 使用Restrictions.sizeEq方式用來查詢EquipmentPO對象中的Collection類型的屬性equipFunctionDevelopPOs的size為1的所有記錄
31
criteria. add (Restrictions.sizeEq("equipFunctionDevelopPOs", 1 ));
32
// 使用sql限定的查詢。注意{alias}.chnname這里是指實際表中的字段名而非屬性名(不區分大小寫)
33
criteria. add (Restrictions.sqlRestriction("{alias}.chnname like (?)"," % 電 % ",Hibernate.STRING));
34
criteria. add (Restrictions.sqlRestriction("{alias}.ENGNAME like (?)"," % ptima % ",Hibernate.STRING));
35
// 如果有多個查詢條件,比如between子句的查詢則如下:
36
BigDecimal [] unitPrice = {new BigDecimal( 402514 ),new BigDecimal( 614891 )};
37
Type [] types = {Hibernate.BIG_DECIMAL,Hibernate.BIG_DECIMAL};
38
criteria. add (Restrictions.sqlRestriction("{alias}.unit_price between (?) and (?)",unitPrice,types));
39
List list = criteria.list();
40
System.out.println("size ===> " + list.size());
41
return list;
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41
