Hibernate中操縱實體對象
一、Session的保存、刪除及更新1.save()方法:將一個對象的屬性取出放入PreparedStatement語句中,然后向數(shù)據(jù)庫中插入n語句。
例:






2.update()方法:
例:





例:將所有學生的姓名前加上一個字符串“xiao”。



5.delete()方法:
負責刪除一個對象例:




1.get()方法:用立即加載的方式發(fā)送SQL語句,并得到已經初始化的對象。





1.使用“?”綁定參數(shù)
例:查找age大于27,名字中有a的所有記錄,打印姓名和年齡。






















students.hbm.xml的源碼如下:
1
<?xml version="1.0" encoding="utf-8"?>
2
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
<hibernate-mapping>
5
<class name="com.zzn.hibernate.Students" table="students" schema="dbo" catalog="hibernate">
6
<id name="sid" type="java.lang.Integer">
7
<column name="sid" />
8
<generator class="native"></generator>
9
</id>
10
<property name="sname" type="java.lang.String">
11
<column name="sname" length="10" not-null="true" />
12
</property>
13
<property name="age" type="java.lang.Integer">
14
<column name="age" not-null="true" />
15
</property>
16
</class>
17
<query name="queryStudents_byAgeAndName">
18
<![CDATA[
19
from Students where age>25:minAge and sname like:likeName
20
]]>
21
</query>
22
</hibernate-mapping>
23
使用命名查詢的源碼如下:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

1
package com.zzn.hibernate;
2
import java.util.List;
3
import org.hibernate.Query;
4
import org.hibernate.Session;
5
import org.hibernate.SessionFactory;
6
import org.hibernate.Transaction;
7
import org.hibernate.cfg.Configuration;
8
9
public class Test3 {
10
@SuppressWarnings("unchecked")
11
public static void main (String[]args){
12
Configuration configuration = new Configuration().configure();
13
SessionFactory sessionFactory = configuration.buildSessionFactory();
14
Session session = sessionFactory.openSession();
15
Transaction transation = session.beginTransaction();
16
17
Students students = new Students();
18
Query query = session.getNamedQuery("queryStudents_byAgeAndName");
19
query.setInteger("minAge", 25);
20
query.setString("likeName", "%a%");
21
List list = query.list();
22
for(int i=0;i<list.size();i++)
23
{
24
students = (Students)list.get(i);
25
System.out.println(students.getSname());
26
System.out.println(students.getAge());
27
System.out.print(students.getSid());
28
}
29
transation.commit();
30
session.close();
31
}
32
}
33
四、查詢緩存:對于有很多select語句,可insert、delete、update、語句較少的情況,使用查詢緩存性能上有一定的改善作用。反之查詢比較少的情況,這方法意義就不大了。
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

(1)在hibernate.cfg.xml中設定hibernate.cache.use_query_cache屬性
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
<hibernate-configuration>
6
<session-factory> 

7
<property name="hibernate.cache.use_query_cache">true</property>
8
</session-factory>
9
</hibernate-configuration>
(2)每次建立Query實例時,執(zhí)行setCacheable(true)。
2

3

4

5

6



7

8

9

1
Students students= new Students();
2
Query query = session.createQuery("from Students");
3
query.setCacheable(true);//準許把查詢放入查詢緩存
4
List list=query.list();
5
for(int i=0;i<list.size();i++){
6
students =(Students) list.get(i);
7
System.out.println(students.getSname());
8
}
9
//一下是再次查詢并打印
10
Query q = session.createQuery("from Students");
11
q.setCacheable(true);
12
List l=q.list();
13
for(int i=0;i<l.size();i++){
14
students =(Students) l.get(i);
15
System.out.println(students.getSname());
16
}
(3)從結果可以看出第一次查詢是從數(shù)據(jù)庫查詢出來的,而第二次則是從緩存中將Query中的結果返回的。
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

五、清除緩存對象:如果在數(shù)據(jù)庫中插入1000000條數(shù)據(jù),再插入500000條時程序拋出例外OutOfMemoryException。因為Hibernate總是把新添加的對象加入到Session級別的緩存中。所以我們必須清除緩存。
(1)clear()方法:
在hibernate.cfg.xml中設置
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
<hibernate-configuration>
6
<session-factory> 

7
<property name="hibernate.jdbc.batch_size">20</property>
8
</session-factory>
9
</hibernate-configuration>
然后在一個合適的頻率下調用flush()清理緩存發(fā)送SQL語句,調用Session的clear()方法清空Session緩存。
2

3

4

5

6



7

8

9

1
//打開Session,開啟事務
2
for(int i=0;i<1000000;i++){
3
Students students = new Students();
4
students.setCardid();
5
session.save(students);
6
if(i%20){
7
//第20個對象保存一次,之后馬上清空Session
8
session.flush();
9
session.clear();
10
}
11
}
12
//提交事務,關閉session
(2)evict()方法:清除緩存中的某個對象
2

3

4

5

6

7

8

9

10

11

12

1
//打開事務Session,開啟事務
2
for(int i=0;i<1000000;i++){
3
Students students = new Students();
4
students.getSid();
5
session.save(students);
6
session.evict(students);//把students對象 清除出Session緩存
7
SessionFactory.evict(Students.class,students.getSid());//把students對象清除出二級緩存
8
}
9
//提交事務,關閉Session
以上說的都是重點要掌握的,還有一些方法如:setEntity();setparameter();setPropertyies();uniqueResult();iterator()等大家有興趣也可以自己去看看。
2

3

4

5

6

7

8

9

posted on 2008-09-28 12:20 生命的綻放 閱讀(434) 評論(0) 編輯 收藏 所屬分類: Hibernate