posts - 14, comments - 15, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          一般在項(xiàng)目開(kāi)發(fā)過(guò)程中,使用比較多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO類,個(gè)人認(rèn)為由于目前所使用的數(shù)據(jù)庫(kù)都是關(guān)系數(shù)據(jù)庫(kù),而hibernate作為一個(gè)ORM,把對(duì)數(shù)據(jù)庫(kù)的操作都對(duì)象化了,更應(yīng)當(dāng)從對(duì)象出發(fā),生成數(shù)據(jù)庫(kù)里面相關(guān)表,這樣更加符合人認(rèn)知事物的習(xí)慣。

          由于hibernate3提供了自帶的工具hbm2ddl,建立根據(jù)你的對(duì)象建立數(shù)據(jù)庫(kù)是一件非常簡(jiǎn)單的事情。

          SchemaExport工具:Hibernatehbm2dll提供SchemaExport工具,給定一個(gè)連接字符串和映射文件,不需輸入其他東西就可以按照持久化類和映射文件自動(dòng)生成數(shù)據(jù)庫(kù)架構(gòu),現(xiàn)在SchemaExport工具還不是很強(qiáng)大,但是一般應(yīng)用足夠了,它還是一個(gè)相當(dāng)原始的API還在不斷改進(jìn)。SchemaExport工具就是把DDL腳本輸出到標(biāo)準(zhǔn)輸出,同時(shí)/或者執(zhí)行DDL語(yǔ)句。SchemaExport工具提供了三個(gè)方法,分別是Drop()Create()、Execute(),前兩個(gè)方法實(shí)質(zhì)是調(diào)用Execute()方法。通常使用Execute()方法來(lái)生成數(shù)據(jù)庫(kù)架構(gòu)的。

          SchemaUpdate工具:Hibernate2.0中新添加SchemaUpdate工具,可以用來(lái)更新數(shù)據(jù)庫(kù)架構(gòu)。但是我覺(jué)得沒(méi)有什么作用,因?yàn)樗荒?span lang="EN-US" style="line-height: 24px; ">Drop現(xiàn)有的表或列,也不能更新現(xiàn)有的列,只能添加新的表和列。如果我需要?jiǎng)h除表或者列或者修改其中列,SchemaUpdate工具就顯得無(wú)能為力了。

          SchemaValidator工具:SchemaValidator工具,可以用來(lái)檢查數(shù)據(jù)庫(kù)架構(gòu)。通過(guò)映射文件中配置的相關(guān)數(shù)據(jù)庫(kù)表及各自的字段屬性來(lái)檢查數(shù)據(jù)庫(kù)中對(duì)應(yīng)表結(jié)構(gòu)是否存在或正確,如果不存在表或字段不一致,則拋出異常。

           

          對(duì)于單純的Hibernate,配置有hibernate.cfg.xml數(shù)據(jù)庫(kù)配置文件,或是其它SSH整合版本中也配置有相關(guān).cfg.xml文件的工程來(lái)說(shuō),這種類型的情況下使用HibernateSchemaExport實(shí)現(xiàn)正向工程比較簡(jiǎn)單。如下例:

          public boolean createTableByBean(String mappingFile) {

                 Configuration cfg = new Configuration().configure(cfgFile);

                 cfg.addFile(mappingFile);

                 boolean flag = true;

                 SchemaExport dbExport = new SchemaExport(cfg);

                 try {

                     dbExport.create(truetrue);

                 catch (Exception e) {

                     flag = false;

                 }

                 return flag;

          }

          而我所做的數(shù)據(jù)遷移工具中使用的是Spring+Hibernate整合框架,而且在數(shù)據(jù)庫(kù)配置中沒(méi)有使用hibernate.propertieshibernate.cfg.xml文件來(lái)配置,

          因?yàn)樵谑褂?span lang="EN-US" style="line-height: 24px; ">HibernateSchemaExport/SchemaUpdate/SchemaValidator工具時(shí),構(gòu)建對(duì)象時(shí)需要HibernateConfiguration對(duì)象實(shí)例作為參數(shù)。如:

          var export = new SchemaExport(cfg); 其中cfg參數(shù)由

          Configuration cfg = new Configuration().configure(cfgFile);建立,configure默認(rèn)讀取hibernate.cfg.xml配置文件(configure無(wú)參函數(shù)),

          也可以通過(guò)傳遞參數(shù)指定自己定義的.cfg.xml文件。我的SessionFactory是借助于Spring提供的

          org.springframework.orm.hibernate3.LocalSessionFactoryBean來(lái)實(shí)現(xiàn)的:

          <bean id="targetSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

              ……

           </bean>

          在沒(méi)有配置.cfg.xml文件的情況下,可以通過(guò)配置的數(shù)據(jù)源的SessionFactory中獲取,如下:

          public class DaoUtil extends HibernateDaoSupport {

          private Configuration cfg;

          private Settings settings;

          public void init() {

               String webRootPath = CommonMethod.getWebRootPath();

               ApplicationContext dataSource_ctx = new FileSystemXmlApplicationContext(webRootPath + "/WEB-INF/datatransferConf/applicationContext-targetDataSource.xml");

               SessionFactoryImplementor sessionFactoryImpl = (SessionFactoryImplementor) dataSource_ctx.getBean("targetSessionFactory");

               LocalSessionFactoryBean localSessionFactory = (LocalSessionFactoryBean) dataSource_ctx.getBean("&targetSessionFactory");

               SessionFactory sessionFactory = (SessionFactory) dataSource_ctx.getBean("targetSessionFactory");

               this.cfg = localSessionFactory.getConfiguration();

               this.settings = sessionFactoryImpl.getSettings();

               super.setSessionFactory(sessionFactory);

          }

          public void createTableFromCfg() {

                if (settings.isAutoCreateSchema()) {

                   new SchemaExport(cfgsettings).create(truetrue);

                else if (settings.isAutoUpdateSchema()) {

                      new SchemaUpdate(cfgsettings).execute(truetrue);

                  else if (settings.isAutoDropSchema()) {

                    new SchemaExport(cfgsettings).drop(truetrue);

                  else if (settings.isAutoValidateSchema()) {

                    new SchemaValidator(cfg).validate();

                  }

          }

          public void createTableFromMapFile(String mappingFile) {

                Resource mappingLocation = new ClassPathResource(mappingFile);

                        try {

                   cfg.addInputStream(mappingLocation.getInputStream());

               catch (MappingException e1) {

                   e1.printStackTrace();

               catch (IOException e1) {

                   e1.printStackTrace();

               }

               createTableFromCfg();

           }

          }

           

          SchemaExport create(script,export)方法根據(jù)持久類和映射文件先刪除架構(gòu)后創(chuàng)建刪除數(shù)據(jù)庫(kù)架構(gòu)。有兩個(gè)參數(shù),第一個(gè)為True就是把DDL語(yǔ)句輸出到控制臺(tái),第二個(gè)為True就是根據(jù)持久類和映射文件先執(zhí)行刪除再執(zhí)行創(chuàng)建操作,經(jīng)過(guò)調(diào)試可以發(fā)現(xiàn)這個(gè)方法其實(shí)質(zhì)是執(zhí)行execute(script,export, false, true)方法。execute(script, export, justDrop, format)方法根據(jù)持久類和映射文件先刪除架構(gòu)后創(chuàng)建刪除數(shù)據(jù)庫(kù)架構(gòu)。有四個(gè)參數(shù),第一個(gè)為True就是把DDL語(yǔ)句輸出到控制臺(tái);第二個(gè)為True就是根據(jù)持久類和映射文件在數(shù)據(jù)庫(kù)中先執(zhí)行刪除再執(zhí)行創(chuàng)建操作;第三個(gè)為false表示不是僅僅執(zhí)行Drop語(yǔ)句還執(zhí)行創(chuàng)建操作,這個(gè)參數(shù)的不同就擴(kuò)展了上面兩個(gè)方法;第四個(gè)參數(shù)為false表示不是格式化輸出DDL語(yǔ)句到控制臺(tái),是在一行輸出的。

           

          Spring配置文件中所定義的LocalSessionFactoryBean實(shí)現(xiàn)了org.springframework.beans.factory.FactoryBean接口,在使用ApplicationContext對(duì)象讀取的時(shí)候可以自動(dòng)轉(zhuǎn)型為多種不同類型的SessionFactory,spring在裝配的時(shí)候如果發(fā)現(xiàn)實(shí)現(xiàn)了org.springframework.beans.factory.FactoryBean接口,就會(huì)使用

          FactoryBean#getObject() 方法返回的對(duì)象裝配,具體的可以看下文檔。如果你想拿到LocalSessionFactoryBean實(shí)例id前面加個(gè)'&'就可以了,在你的配置文件中BeanFactory.getBean('&sessionFactory')拿到的就是LocalSessionFactoryBean的實(shí)例。

          我要啦免费统计
          主站蜘蛛池模板: 衡水市| 云安县| 凤台县| 延吉市| 南投县| 环江| 新营市| 鄄城县| 吴江市| 库尔勒市| 阿克陶县| 观塘区| 禄丰县| 德格县| 岳阳市| 响水县| 定兴县| 长顺县| 阳谷县| 贵南县| 柘城县| 那曲县| 格尔木市| 颍上县| 岑溪市| 澎湖县| 玉田县| 固镇县| 阆中市| 达孜县| 金沙县| 水富县| 聂拉木县| 千阳县| 德江县| 鹤峰县| 沈阳市| 金平| 湛江市| 赣榆县| 安塞县|