当单独使用hibernate时可以利用下面的代码可以重新生成数据库表。Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(true, true);当hibernate与spring集成后,以前的hibernate.cfg.xml文件内容已经放在ApplicationContext.xml中了,也就是说hibernate.cfg.xml文件已经不存在了,那怎么利用spring重新生成数据库表呢?谢谢!

解决方案 »

  1.   

    问题在于怎么获取到Configuration对象,至于怎么获取我就不知道了。
      

  2.   

    首先谢谢你的回答,我也想过了,看了spring几个常用的对象,没有发现可以得到Configuration对象的方法。
      

  3.   

    看一下spring hibernate的整合吧 里面有个属性 可以配置 在服务启动的时候可以自动生成表
      

  4.   


    <prop key="hibernate.hbm2ddl.auto">update</prop>
      

  5.   

    我知道那个配置,那是spring初始化的时候会生成数据库表,但我现在是想能不能重新生成数据库表,也就是说当spring启动以后,可以通过一个按钮或链接重新生成数据库表(相当于清空数据库表的内容)。
      

  6.   


    是否可以把Configuration对象声明为一个<bean>?对它的属性进行注入?
      

  7.   

    这样可以获取Configuration对象,但是由于没有hibernate.cfg.xml配置文件(已经集成到spring配置文件中去了), 重新生成数据库表仍然失败!大家有没有办法啊?
      

  8.   

    Configuration对象为一个<bean>的时候,它还是有自己的属性的,比如说方言,比如说mapping的路径,比如说URL,应该可以使用property直接注入,
      

  9.   

    Hibernate整合Spring后,如何使用SchemaExport生成数据库表
    3人收藏此文章, 我要收藏 发表于2个月前(2012-08-18 20:16) , 已有161次阅读 共0个评论 SchemaExport生成数据库表
    一.Hibernate原生状态
     view sourceprint?
    1 Configuration cfg = new Configuration().configure();  2    3 SchemaExport export = new SchemaExport(cfg);  4    5 export.create(true, true); 
     二.Hibernate整合Spring       1.使用hibernate.cfg.xml原生配置              hibernate.cfg.xml同原生一样编写              在Spring主配置文件applicationContext中,引入hibernate.cfg.xml             使用SchemaExport生成数据库表的代码同上一致。view sourceprint?
    01 Spring applicationContext.xml  02    03 <bean id="sessionFactory" 04    05    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  06    07       <property name="configLocation" 08    09         value="file:src/hibernate.cfg.xml">  10    11       </property>  12    13 </bean>        2.不使用hibernate.cfg.xml,在Spring的主配置文件applicationContext.xml中配置              完全不编写hibernate.cfg.xml,全部都在applicationContext.xml中配置   
    view sourceprint?
    01 ClassPathResource ac = new ClassPathResource("applicationContext.xml");  02   03      XmlBeanFactory xbf = new XmlBeanFactory(ac);  04   05      //注意: &sessionFactory ,一定要包含 & ,不加Spring返回的是Hibernate下的SessionFactoryImpl类  06   07      LocalSessionFactoryBean lsfb=(LocalSessionFactoryBean) xbf.getBean("&sessionFactory");  08   09      Configuration cfg=lsfb.getConfiguration();  10   11      SchemaExport export=new SchemaExport(cfg);  12   13      export.create(true, false); view sourceprint?01 <!-- 配置数据源--> 02   03  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  04   05     <property name="driverClassName" value="${jdbc.driverClassName}"/>  06   07       <property name="url" value="${jdbc.url}"/>  08   09       <property name="username" value="${jdbc.username}"/>  10   11       <property name="password" value="${jdbc.password}"/>  12   13  </bean>  14   15    16   17  <!-- 配置sessionfactory,该配置替代了hibernate.cfg.xml的配置 --> 18   19  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  20   21     <property name="dataSource" ref="dataSource"></property>  22   23     <property name="mappingResources">  24   25       <list>  26   27          <value>xxx/xxx/model/User.hbm.xml</value>  28   29       </list>  30   31     </property>  32   33     <property name="hibernateProperties">  34   35       <props>  36   37          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  38   39          <prop key="hibernate.show_sql">true</prop>  40   41          <prop key="hibernate.format_sql">true</prop>  42   43       </props>  44   45     </property>  46   47  </bean>