Posted on 2006-11-10 11:32
城市劣人 閱讀(2835)
評論(3) 編輯 收藏
已經轉移到
好·色之徒--我的博客、我的生活通常如果采用Spring來管理hibernate.cfg.xml開發時:
Spring的配置文件中有SessionFactory的配置,主要是通過Spring來建立SessionFactory環境的。定義了數據源dataSource、mappingResources、hibernateProperties,舉例如下:
com/iplan/portal/start/pojo/hbm/User.hbm.xml
org.hibernate.dialect.HSQLDialecttrue1trueorg.hibernate.cache.EhCacheProvider 3似乎hibernate.cfg.xml沒有存在的必要了,因為它的功能上面都已經體現了。其實不然,上面的配置適合開發時用,屬性mappingResources可以隨意增刪需要測試的部分。
部署時,可以替換屬性mappingResources,因為.hbm.xml文件可能數量多、位置雜,何不用hibernate.cfg.xml統一管理呢,也使得spring配置文件“干凈”一些。配置如下:
classpath:hibernate.cfg.xmlhibernate.cfg.xml中則設置.hbm.xml內容。
另外:在開發時hibernate.cfg.xml的配置可以是這樣:
org.hibernate.dialect.HSQLDialectorg.hsqldb.jdbcDriverjdbc:hsqldb:hsql://localhost:9003/hsqldbsatrue感覺上和上面的spring配置有所重疊的地方,不過它主要是用于開發時的設置。可以采用hibernate中的工具類(hbm2ddl)SchemaExport,通過hbm來生成ddl。
管理類如下:
public class ManageDB {
private SessionFactory sessionFactory;
private Session session;
{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void execute() {
Configuration conf = new Configuration().configure("/hibernate.cfg.xml");
SchemaExport dbExport=new SchemaExport(conf);
dbExport.create(true, true);
}
public static void main(String[] args){
ManageDB db =new ManageDB();
db.execute();
}
}
這樣通過運行ManageDB類,對hibernate.cfg.xml中設置的hbm來生成ddl。生成以后,在spring中的mappingResources中對應追加hbm,用于開發測試。進行下一步時,在hibernate.cfg.xml中追加相應的hbm,刪除已經生成ddl的hbm... 到項目最終,spring替換上面提到的configLocation屬性,hibernate.cfg.xml中則只保留所有的hbm就可以了。
Spring hibernate開發時的一些小建議??
有個論壇