在hibernate.cfg.xml里面加上
<property name="hibernate.hbm2ddl.auto">create</property>

解决方案 »

  1.   

    我直接在Spring-config.xml中配置呢?是不是在定义sessionFactory的那段中加入
    <property name="hibernate.hbm2ddl.auto">create</property> 就可以了?请帮忙看一下下面的配置中有没有实现了该功能?我是新学员,一直找不出哪里使得自动创建了数据表,非常感谢!<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref local="dataSource"/>
            </property>
            <property name="mappingDirectoryLocations">
                <list>
                   <value>WEB-INF/classes/dragonchant/application/foto/model</value>
                </list>
            </property>        
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    
            <prop key="hibernate.dbcp.whenExhaustedAction">1</prop>
            <prop key="hibernate.dbcp.maxIdle">20</prop>
            <prop key="hibernate.dbcp.maxActive">20</prop>
            <prop key="hibernate.dbcp.maxWait">200</prop>
    </props>
    </property>
    </bean>
      

  2.   

    在eclipse中做了个spring的应用例子,在eclipse中能够正常运行并能自动在数据库中创建表,但是将程序拷贝到tomcat/webapps目录下单独运行时,却报错说mysql驱动找不到,我弄了个驱动放在lib目录下,但结果还是一样,请问这是怎么回事?mysql驱动jar包为:mysql-connector-java-3.0.17-ga.jarspring-config.xml为:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
                    "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <!-- ================================================================== -->
        <!-- DataSource                                                         -->
        <!-- ================================================================== -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
                <value>jdbc:mysql://localhost:3306/infos</value>
            </property>
            <property name="username"><value>admin</value></property>
            <property name="password"><value>admin</value></property>
        </bean> 
        
        <!-- ================================================================== -->
        <!-- Choose the dialect that matches your "dataSource" definition       -->
        <!-- ================================================================== -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref local="dataSource"/>
            </property>
            <property name="mappingDirectoryLocations">
                <list>
                   <value>WEB-INF/classes/yahai/hibernate/model</value>
                </list>
            </property>  
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    
                    <prop key="hibernate.dbcp.whenExhaustedAction">1</prop>
                    <prop key="hibernate.dbcp.maxIdle">20</prop>
                    <prop key="hibernate.dbcp.maxActive">20</prop>
                    <prop key="hibernate.dbcp.maxWait">200</prop>
                </props>
            </property>
        </bean>
       
    </beans>
      

  3.   

    SchemaExport:
    <?xml version="1.0"?>
    <project name="Harnessing Hibernate: The Developer's Notebook"
             default="db" basedir=".">  <!-- Set up properties containing important project directories -->
      <property name="source.root" value="src"/>
      <property name="class.root" value="classes"/>
      <property name="lib.dir" value="lib"/>
      <property name="data.dir" value="data"/>  <!-- Set up the class path for compilation and execution -->
      <path id="project.class.path">
          <!-- Include our own classes, of course -->
          <pathelement location="${class.root}" />
          <!-- Include jars in the project library directory -->
          <fileset dir="${lib.dir}">
            <include name="*.jar"/>
          </fileset>
      </path>  <target name="db" description="Runs HSQLDB database management UI
    against the database file--use when application is not running">
          <java classname="org.hsqldb.util.DatabaseManager"
                fork="yes">
             <classpath refid="project.class.path"/>
             <arg value="-driver"/>
             <arg value="org.hsqldb.jdbcDriver"/>
             <arg value="-url"/>
             <arg value="jdbc:hsqldb:${data.dir}/music"/>
             <arg value="-user"/>
             <arg value="sa"/>
          </java>
      </target>  <!-- Teach Ant how to use Hibernate's code generation tool -->
      <taskdef name="hbm2java"
               classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
               classpathref="project.class.path"/>  <!-- Generate the java code for all mapping files in our source tree -->
      <target name="codegen"
              description="Generate Java source from the O/R mapping files">
        <hbm2java output="${source.root}">
          <fileset dir="${source.root}">
            <include name="**/*.hbm.xml"/>
          </fileset>
        </hbm2java>
      </target>  <!-- Create our runtime subdirectories and copy resources into them -->
      <target name="prepare" description="Sets up build structures">
        <mkdir dir="${class.root}"/>    <!-- Copy our property files and O/R mappings for use at runtime -->
        <copy todir="${class.root}" >
          <fileset dir="${source.root}" >
            <include name="**/*.properties"/>
            <include name="**/*.hbm.xml"/>
          </fileset>
        </copy>
      </target>  <!-- Compile the java source of the project -->
      <target name="compile" depends="prepare"
              description="Compiles all Java classes">
        <javac srcdir="${source.root}"
               destdir="${class.root}"
               debug="on"
               optimize="off"
               deprecation="on">
          <classpath refid="project.class.path"/>
        </javac>
      </target>  <!-- Generate the schemas for all mapping files in our class tree -->
      <target name="schema" depends="compile"
              description="Generate DB schema from the O/R mapping files">    <!-- Teach Ant how to use Hibernate's schema generation tool -->
        <taskdef name="schemaexport"
                 classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
                 classpathref="project.class.path"/>    <schemaexport properties="${class.root}/hibernate.properties"
                      quiet="no" text="no" drop="no">
          <fileset dir="${class.root}">
            <include name="**/*.hbm.xml"/>
          </fileset>
        </schemaexport>
      </target></project>
      

  4.   

    当然用程序也好:
    public class InitDB {
    static Session session;public static void main(String[] args) {
    Configuration config = null;
    Transaction tx = null;try {
    config = new Configuration().configure(new File("hibernate.cfg.xml"));
    System.out.println("Creating tables...");
    SchemaExport schemaExport = new SchemaExport(config);
    schemaExport.create(true, true);
    System.out.println("Table created.");
    SessionFactory sessionFactory = config.buildSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();
    tx.commit();} catch (HibernateException e) {
    e.printStackTrace();
    try {
    tx.rollback();
    } catch (HibernateException e1) {
    e1.printStackTrace();
    }
    } finally {}
    }
    }
      

  5.   

    请问:如果是用mysql数据库,下面代码应该做如何改动?我弄不明白其中的 classname="org.hsqldb.util.DatabaseManager" 。它是不是一个配置数据库连接的类?可是如果是MYSQL的话,应该用什么类呢?或者需要什么JAR包呢?我在MYSQL连接的JAR包里找不到,能给我解释一下吗?非常感谢!<target name="db" description="Runs HSQLDB database management UI
    against the database file--use when application is not running">
          <java classname="org.hsqldb.util.DatabaseManager"
                fork="yes">
             <classpath refid="project.class.path"/>
             <arg value="-driver"/>
             <arg value="org.hsqldb.jdbcDriver"/>
             <arg value="-url"/>
             <arg value="jdbc:hsqldb:${data.dir}/music"/>
             <arg value="-user"/>
             <arg value="sa"/>
          </java>
    </target>