java - 如何让hibernate自动生成数据库?

标签 java hibernate schema

我的 hibernate.cfg.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateSimpleDB</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>

        <property name="transaction.factory_class">org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <property name="hibernate.cache.use_second_level_cache">true</property>

        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

        <property name="current_session_context_class">thread</property>

        <mapping resource="com/masterhibernate/SimpleHibernateDemo/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

我希望 hibernate 自动创建数据库 hibernateSimpleDB 。 但它并没有这样做。

我知道它会自动创建表,但我想自动创建数据库和表。

可能吗?

最佳答案

看看这个 http://mojo.codehaus.org/hibernate3-maven-plugin/hbm2ddl-mojo.html

并创建表格。

检查此 Hibernate 属性。

             <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

这就是神奇的一行:

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

关于java - 如何让hibernate自动生成数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135509/

相关文章:

java - 没有通过 FileInputStream 获取文件?

python - 从 Apache Spark 中的模式获取数据类型列表

sql - 如何将表架构和约束复制到不同数据库的表?

ruby-on-rails - 使用工具/插件/脚本可视化 Rails schema.rb

java - 我自己的Java编译器和字节码: Problems invoking functions

java - 打印指定的扩展文件

java - 从4.3.11.Final版本切换到5.0.1.Final导致编译报错

java - Hibernate_sequence 不存在于 hibernate envers

java - 如何创建两个具有相同实体的类?

java - Spring WebSockets @SendTo 映射中的路径变量