示例代码如下:
- package com.hb3.pack_01;
- import java.util.Iterator;
- import org.hibernate.Query;
- import org.hibernate.SQLQuery;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_01.model.User;
- public class BusinessService {
- public static void main(String[] args) {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- String sql = "select {user.*} from User user where user.age > ?";
- SQLQuery sqlQuery = session.createSQLQuery(sql);
- sqlQuery.setParameter(0, 20);
- sqlQuery.addEntity("user", User.class);
- Iterator<?> iterator = sqlQuery.list().iterator();
- while(iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getAge() + "\t" + user.getName());
- }
-
- Query query = session.getNamedQuery("com.hb3.pack_01.model.User.SQLQueryUser");
- query.setInteger("age", 20);
- iterator = query.list().iterator();
- while(iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getAge() + "\t" + user.getName());
- }
- session.close();
- sessionFactory.close();
- }
- }
修改User.hbm.xml文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.hb3.pack_01.model.User" table="user"
- dynamic-insert="true"
- dynamic-update="true"
- >
- <id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
- <generator class="native" />
- </id>
-
- <property name="name" column="name" type="java.lang.String" />
-
- <property name="age" column="age" type="java.lang.Integer" />
- </class>
-
- <query name="com.hb3.pack_01.model.User.QueryUser">
- <![CDATA[
- from User as user where user.age > :minAge
- ]]>
- </query>
-
- <sql-query name="com.hb3.pack_01.model.User.SQLQueryUser">
- <![CDATA[
- select {user.*} from User user where user.age > :age
- ]]>
- <return alias="user" class="com.hb3.pack_01.model.User"/>
- </sql-query>
-
- </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文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.hb3.pack_01.model.User" table="user"
- dynamic-insert="true"
- dynamic-update="true"
- >
- <id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
- <generator class="native" />
- </id>
-
- <property name="name" column="name" type="java.lang.String" />
-
- <property name="age" column="age" type="java.lang.Integer" />
-
- <sql-insert>
-
- INSERT INTO user VALUES (?, ?)
- </sql-insert>
- <sql-update>
- UPDATE user SET name=?, age=?, WHERE id=?
- </sql-update>
- <sql-delete>
- DELETE FROM user WHERE id=?
- </sql-delete>
-
- </class>
-
- <query name="com.hb3.pack_01.model.User.QueryUser">
- <![CDATA[
- from User as user where user.age > :minAge
- ]]>
- </query>
-
- <sql-query name="com.hb3.pack_01.model.User.SQLQueryUser">
- <![CDATA[
- select {user.*} from User user where user.age > :age
- ]]>
- <return alias="user" class="com.hb3.pack_01.model.User"/>
- </sql-query>
-
- </hibernate-mapping>
示例代码:
- package com.hb3.pack_01;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_01.model.User;
- public class BusinessService {
- public static void main(String[] args) {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
- User user = new User();
- user.setName("yanfeng");
- user.setAge(21);
- Transaction tx = session.beginTransaction();
- session.save(user);
- tx.commit();
-
- session.close();
- sessionFactory.close();
- }
- }
测试结果如下:
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教程