5.hibernate比较有用的特性

离线查询方式

public List<Product> getProducts(DetachedCriteria dc) {

       Session session=HibernateSessionFactory.getSessionFactory().getCurrentSession();

       Criteria criteria=dc.getExecutableCriteria(session);

       List<Product> list=criteria.list();

       return list;

    }

使用:

DetachedCriteria dc = DetachedCriteria.forClass(User.class);

       dc.add(Restrictions.eq("regDate", new Date()));

       dc.add(Restrictions.eq("userName", "dhjk"));

ProductDaoImpl productDaoImpl =new     ProductDaoImpl();

List<Product> list=productDaoImpl.getProducts(dc);

 

l         用hibernate配置文件生成数据库

使用hibernate可以不先创建数据库。Hibernate会跟据映射文件自动生成数据库。生成代码为

Configuration configuration=HibernateSessionFactory.getConfiguration();

 SchemaExport schemaExport=new SchemaExport(configuration);

schemaExport.create(true, true);

一个Configuration对应一个配置文件,所以hibernate可以有多个类似hibernate.cfg.xml的配置文件。然后跟据这些配置文件生成相应的SessionFactory和Configuration.

Hibernate这一特性正体现了面向对象的特点,很有用,对于数据库的迁移只要更改一个配置文件就可以了。

posted @ 2010-05-03 17:21  沉兮  阅读(478)  评论(0编辑  收藏  举报