示例代码如下:

  1. package com.hb3.pack_01;

  2. import java.util.Iterator;

  3. import org.hibernate.Query;
  4. import org.hibernate.SQLQuery;
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.cfg.Configuration;

  8. import com.hb3.pack_01.model.User;

  9. public class BusinessService {

  10.     public static void main(String[] args) {
  11.         
  12.         Configuration config = new Configuration().configure();
  13.         SessionFactory sessionFactory = config.buildSessionFactory();      
  14.         Session session = sessionFactory.openSession();
  15.         
  16.         String sql = "select {user.*} from User user where user.age > ?";
  17.         SQLQuery sqlQuery = session.createSQLQuery(sql);
  18.         sqlQuery.setParameter(020);
  19.         sqlQuery.addEntity("user", User.class);
  20.         Iterator<?> iterator = sqlQuery.list().iterator();
  21.         while(iterator.hasNext()) {
  22.             User user = (User) iterator.next();
  23.             System.out.println(user.getAge() + "\t" + user.getName());
  24.         }
  25.         
  26.         Query query = session.getNamedQuery("com.hb3.pack_01.model.User.SQLQueryUser");
  27.         query.setInteger("age"20);
  28.         iterator = query.list().iterator();
  29.         while(iterator.hasNext()) {
  30.             User user = (User) iterator.next();
  31.             System.out.println(user.getAge() + "\t" + user.getName());
  32.         }

  33.         session.close();
  34.         sessionFactory.close();
  35.     }
  36. }

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

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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

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

示例代码:

  1. package com.hb3.pack_01;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;

  6. import com.hb3.pack_01.model.User;

  7. public class BusinessService {

  8.     public static void main(String[] args) {
  9.         
  10.         Configuration config = new Configuration().configure();
  11.         SessionFactory sessionFactory = config.buildSessionFactory();      
  12.         Session session = sessionFactory.openSession();

  13.         User user = new User();
  14.         user.setName("yanfeng");
  15.         user.setAge(21);
  16.         Transaction tx = session.beginTransaction();
  17.         session.save(user); 
  18.         tx.commit(); 
  19.         
  20.         session.close();
  21.         sessionFactory.close();
  22.     }
  23. }

测试结果如下:

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文。



ExtJS教程- Hibernate教程- Struts2 教程- Lucene教程