测试代码:
- 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();
-
- //insert
- User user = new User();
- user.setName("chenyan");
- //user.setAge(new Integer(23));
- Transaction tx = session.beginTransaction();
- session.save(user);
- tx.commit();
-
- //get
- user = (User) session.get(User.class, user.getId());
- System.out.println(user.getAge());
-
- //update
- user.setAge(new Integer(24));
- tx= session.beginTransaction();
- session.update(user);
- tx.commit();
- //load
- user = (User) session.load(User.class, user.getId());
- System.out.println(user.getAge());
-
- //delete
- tx = session.beginTransaction();
session.delete(user);
tx.commit();
-
- session.close();
- sessionFactory.close();
- }
- }
执行结果:
11:50:50,750 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
11:50:51,156
WARN EhCacheProvider:93 - Could not find configuration
[org.hibernate.cache.UpdateTimestampsCache]; using defaults.
11:50:51,171 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: insert into user (name, age) values (?, ?)
null
Hibernate: update user set name=?, age=? where id=?
24
修改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">
- <generator class="native" />
- </id>
-
- <property name="name" column="name" type="java.lang.String" />
-
- <property name="age" column="age" type="java.lang.Integer" />
- </class>
- </hibernate-mapping>
运行结果如下:
12:46:32,062 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
12:46:32,468
WARN EhCacheProvider:93 - Could not find configuration
[org.hibernate.cache.UpdateTimestampsCache]; using defaults.
12:46:32,500 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: insert into user (name) values (?)
null
Hibernate: update user set age=? where id=?
24
Hibernate: delete from user where id=?
可见:
dynamic-insert和dynamic-update设成true后,分别表示生成的SQL中只插入非空字段以及只修改改变过的字段。
另外,hibernate还提供了saveOrUpdate()方法,因为这个方法很容易出错,故在后续章节着重讲述。
关于查询:
- package com.hb3.pack_01;
- import java.util.Iterator;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.criterion.Restrictions;
- 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();
-
- Criteria criteria = session.createCriteria(User.class);
- List<?> users = criteria.list();
- Iterator<?> iterator = users.iterator();
- System.out.println("id "t name/age");
- while (iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getId() + " "t " + user.getName() + "/" + user.getAge());
- }
-
- //criteria.add(Expression.eq("name", "shenbin"));
- criteria.add(Restrictions.eq("name", "shenbin"));
- users = criteria.list();
- iterator = users.iterator();
- System.out.println("id "t name/age");
- while (iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getId() + " "t " + user.getName() + "/" + user.getAge());
- }
-
- System.out.println("====================================================");
-
- Query query = session.createQuery("from User");
- users = query.list();
- iterator = users.iterator();
- System.out.println("id "t name/age");
- while (iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getId() + " "t " + user.getName() + "/" + user.getAge());
- }
-
- query = session.createQuery("from User user where user.name like ?");
- query.setParameter(0, "shenbin");
- users = query.list();
- iterator = users.iterator();
- System.out.println("id "t name/age");
- while (iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getId() + " "t " + user.getName() + "/" + user.getAge());
- }
- session.close();
- sessionFactory.close();
- }
- }
执行结果:
13:05:43,031 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
13:05:43,453
WARN EhCacheProvider:93 - Could not find configuration
[org.hibernate.cache.UpdateTimestampsCache]; using defaults.
13:05:43,484 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_
id name/age
1 shenbin/29
2 chenyan/24
3 chenyan/24
4 chenyan/24
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from user this_ where this_.name=?
id name/age
1 shenbin/29
================================================
Hibernate: select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from user user0_
id name/age
1 shenbin/29
2 chenyan/24
3 chenyan/24
4 chenyan/24
Hibernate: select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from user user0_ where user0_.name like ?
id name/age
1 shenbin/29
以上分别演示了使用Criteria对SQL的分装以及透过HQL来进行查询。
注意:
代码中如下部分
//criteria.add(Expression.eq("name", "shenbin"));
criteria.add(Restrictions.eq("name", "shenbin"));
在hibernate3.3以后的版本,可能Expression类会被废除,为了保证代码的向后兼容,请使用Restrictions替代。
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程