程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(十) SQL支援

hibernate3學習筆記(十) SQL支援

編輯:關於JAVA

還是最初的那個例子:http://blog.csdn.net/kunshan_shenbin/archive/2008/09/02/2864883.aspx

示例代碼如下:

1.package com.hb3.pack_01;
2.
3.import java.util.Iterator;
4. 5.import org.hibernate.Query;
6.import org.hibernate.SQLQuery;
7.import org.hibernate.Session;
8.import org.hibernate.SessionFactory;
9.import org.hibernate.cfg.Configuration;
10. 11.import com.hb3.pack_01.model.User;
12. 13.public class BusinessService {
14. 15. public static void main(String[] args) {
16.
17. Configuration config = new Configuration().configure();
18. SessionFactory sessionFactory = config.buildSessionFactory();
19. Session session = sessionFactory.openSession();
20.
21. String sql = "select {user.*} from User user where user.age > ?";
22. SQLQuery sqlQuery = session.createSQLQuery(sql);
23. sqlQuery.setParameter(0, 20);
24. sqlQuery.addEntity("user", User.class);
25. Iterator<?> iterator = sqlQuery.list().iterator();
26. while(iterator.hasNext()) {
27. User user = (User) iterator.next();
28. System.out.println(user.getAge() + "\t" + user.getName());
29. }
30.
31. Query query = session.getNamedQuery("com.hb3.pack_01.model.User.SQLQueryUser");
32. query.setInteger("age", 20);
33. iterator = query.list().iterator();
34. while(iterator.hasNext()) {
35. User user = (User) iterator.next();
36. System.out.println(user.getAge() + "\t" + user.getName());
37. }
38. 39. session.close();
40. sessionFactory.close();
41. }
42.}

修改User.hbm.xml文件如下:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. <class name="com.hb3.pack_01.model.User" table="user"
9. dynamic-insert="true"
10. dynamic-update="true"
11. > 12. <id name="id" column="id" type="java.lang.Integer" unsaved-value="null"> 13. <generator class="native" /> 14. </id> 15.
16. <property name="name" column="name" type="java.lang.String" /> 17.
18. <property name="age" column="age" type="java.lang.Integer" /> 19. </class> 20.
21. <query name="com.hb3.pack_01.model.User.QueryUser"> 22. <![CDATA[
23. from User as user where user.age > :minAge
24. ]]> 25. </query> 26.
27. <sql-query name="com.hb3.pack_01.model.User.SQLQueryUser"> 28. <![CDATA[
29. select {user.*} from User user where user.age > :age
30. ]]>
31. <return alias="user" class="com.hb3.pack_01.model.User"/> 32. </sql-query> 33.
34.</hibernate-mapping>

運行結果:

17:20:40,859  WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/D:/Java/MyEclipse%206.0/workspace/hb3demo/ehcache-1.2.3.jar!/ehcache-failsafe.xml
17:20:41,281 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
17:20:41,312 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: select user.id as id0_0_, user.name as name0_0_, user.age as age0_0_ from User user where user.age > ?
28 shenbin
24 chenyan
23 shenyi
23 yangye
22 chenyong
Hibernate: select user.id as id0_0_, user.name as name0_0_, user.age as age0_0_ from User user where user.age > ?
28 shenbin
24 chenyan
23 shenyi
23 yangye
22 chenyong

另外,我們還可以自定義insert、update、delete時的SQL文而不使用Hibernate3自動生成的SQL文。

修改User.hbm.xml文件如下:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. <class name="com.hb3.pack_01.model.User" table="user"
9. dynamic-insert="true"
10. dynamic-update="true"
11. > 12. <id name="id" column="id" type="java.lang.Integer" unsaved-value="null"> 13. <generator class="native" /> 14. </id> 15.
16. <property name="name" column="name" type="java.lang.String" /> 17.
18. <property name="age" column="age" type="java.lang.Integer" /> 19.
20. <sql-insert> 21. <!-- INSERT INTO user (name, age) VALUES (?, ?) --> 22. INSERT INTO user VALUES (?, ?)
23. </sql-insert> 24. <sql-update> 25. UPDATE user SET name=?, age=?, WHERE id=?
26. </sql-update> 27. <sql-delete> 28. DELETE FROM user WHERE id=?
29. </sql-delete> 30.
31. </class> 32.
33. <query name="com.hb3.pack_01.model.User.QueryUser"> 34. <![CDATA[
35. from User as user where user.age > :minAge
36. ]]> 37. </query> 38.
39. <sql-query name="com.hb3.pack_01.model.User.SQLQueryUser"> 40. <![CDATA[
41. select {user.*} from User user where user.age > :age
42. ]]>
43. <return alias="user" class="com.hb3.pack_01.model.User"/> 44. </sql-query> 45.
46.</hibernate-mapping>

示例代碼:

1.package com.hb3.pack_01;
2.
3.import org.hibernate.Session;
4.import org.hibernate.SessionFactory;
5.import org.hibernate.Transaction;
6.import org.hibernate.cfg.Configuration;
7. 8.import com.hb3.pack_01.model.User;
9. 10.public class BusinessService {
11. 12. public static void main(String[] args) {
13.
14. Configuration config = new Configuration().configure();
15. SessionFactory sessionFactory = config.buildSessionFactory();
16. Session session = sessionFactory.openSession();
17. 18. User user = new User();
19. user.setName("yanfeng");
20. user.setAge(21);
21. Transaction tx = session.beginTransaction();
22. session.save(user);
23. tx.commit();
24.
25. session.close();
26. sessionFactory.close();
27. }
28.}

測試結果如下:

17:29:03,781 WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Java/MyEclipse%206.0/workspace/hb3demo/ehcache-1.2.3.jar!/ehcache-failsafe.xml
17:29:04,328 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
17:29:04,359 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: insert into user (name, age) values (?, ?)

可見,在實行保存操作時的SQL確實是我們在配置文件中定義的SQL文。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved