最近给客户开发的一个项目,需要在不更改代码的情况下提供对客户建表的支持,(所建的表都是用于查询用的,只有表名字不同而字段完全相同---方便维护) 由于系统应用了hibernate,所以在建表同时也要建立持久化对象以及对这些对象注册,数据库我是采用的sql server 2005, 
例如:一。 建表t0371 需要生成 
1.t0371.class  
2.生成t0371.hbm.xml 
3.在Hibernate'config里面注册持久化类 
4.通知SessionFactory持久化类的新增 
二。 建表t0391 需要生成 
1.t0391.class  
2.生成t0391.hbm.xml 
3.在Hibernate'config里面注册持久化类 
4.通知SessionFactory持久化类的新增 
写个专门的方法来实现 这对于我这个刚参加工作的来说太困难了 
是不是我想偏了 也许有更好的方法能够实现功能   

解决方案 »

  1.   

    package com.mit.cooperate.core.hibernate;  
      
    import junit.framework.TestCase;  
      
    import java.net.URL;  
    import java.util.ArrayList;  
      
    import org.apache.commons.beanutils.PropertyUtils;  
      
    import org.hibernate.Session;  
    import org.hibernate.cfg.Configuration;  
    import org.hibernate.SessionFactory;  
    import org.hibernate.tool.hbm2ddl.SchemaExport;  
    import org.hibernate.impl.SessionFactoryImpl;  
    import org.hibernate.mapping.PersistentClass;  
    import org.hibernate.Transaction;  
      
    import com.mit.cooperate.core.asm.*;  
    import com.mit.cooperate.core.asm.render.*;  
      
    public class HibernateTest extends TestCase {  
          
        private Configuration config;  
        private SessionFactory factory;  
          
        public void setUp()  
        {  
            URL  url = this.getClass().getResource("/com/mit/cooperate/core/hibernate/hibernate.cfg.xml");  
            config = new Configuration().configure(url);  
            factory = config.buildSessionFactory();  
        }  
          
        public void testBuild() throws Exception  
        {  
            //持久类对象描述  
            RenderClass rc = new RenderClass();  
            ArrayList list = new ArrayList();  
              
            RenderProperty property = new RenderProperty();  
            //添加主键  
            property.setName("oid");  
            property.setField("oid");  
            property.setLength(new Integer(15));  
            property.setPrimary(true);  
            property.setType(Long.class.getName());  
            property.setSequence("SEQ_PERSON");  
              
            list.add(property);  
            //添加一个name字段  
            property = new RenderProperty();  
            property.setName("name");  
            property.setType(String.class.getName());  
            property.setField("name");  
            property.setLength(new Integer(20));  
              
            list.add(property);  
              
            rc.setProperties(list);  
            //类名  
            rc.setClassName("com.mit.test.Person");  
            rc.setTableName("person");  
            //开始生成class  
            POBuildUtil util = new POBuildUtil();  
            util.build(rc.getClassName(),"E:\\cpc\\source\\cooperateCore\\com\\mit\\test\\Person.class",list);  
            //实例化一个person  
            Object person = Class.forName("com.mit.test.Person").newInstance();//hbmcls.newInstance();  
              
            //开始生成hbm.xml  
            FreeerRender render = new FreeerRender();  
            render.render(rc, Templates.TEMPLATE_HIBERNATE3, "E:\\cpc\\source\\cooperateCore\\com\\mit\\test\\person.hbm.xml");  
            URL  url = this.getClass().getResource("/com/mit/test/person.hbm.xml");  
            config.addURL(url);  
            //更新hibernate.cfg.xml  
            HibernateUtil.updateHbmCfg( this.getClass().getResource("/com/mit/cooperate/core/hibernate/hibernate.cfg.xml"), "com/mit/test/person.hbm.xml");  
              
            PersistentClass model = config.getClassMapping("com.mit.test.Person");  
            //sessionFactory哪下子,快接纳person爷爷进去  
            ((SessionFactoryImpl)factory).addPersistentClass(model, config.getMapping());  
            //生成数据库  
            SchemaExport export = new SchemaExport(config,((SessionFactoryImpl)factory).getSettings());  
            export.execute(true, true,false,true);  
            //测试一下,随便给个名字什么的  
            PropertyUtils.setProperty(person, "name", "chenzhi");  
            Session session = factory.openSession();  
            Transaction tran = session.beginTransaction();  
            try  
            {  
                //保存  
                session.save(person);  
                tran.commit();  
            }  
            catch (Exception e)  
            {  
                e.printStackTrace();  
                tran.rollback();  
            }  
            finally  
            {  
                session.close();  
            }  
        }  
          
        public void tearDown()  
        {  
            factory.close();  
        }  
          
          
    }  
      

  2.   

    能不能加点注释啊  POBuildUtil FreeerRender 都是什么啊 应用到什么类吗
      

  3.   

    不要用hibernate了,如果是动态的话,还不如自己用sql搞定
      

  4.   

    LZ是想做hibernate那种自动化生成工具
      

  5.   

    介个吗  项目用的是hibernate 还是不改的好 
      

  6.   

    hibernate 不是有自动见表的功能么?不知道能不能实现你所需要的:<property name="hbm2ddl.auto">create</property>
      

  7.   

    sorry 自动建表的同时已经创建的实体类。。
      

  8.   

    http://blog.csdn.net/fangaoxin/article/details/6317274网上找的一些类似方法
      

  9.   

    http://blog.csdn.net/fangaoxin/article/details/6317274好像是又一种解决方法 
    自己还没解决 希望对看帖的人有帮助 也能帮我一把