【问题标题】:How to tell hibernate to generate database automatically?如何告诉hibernate自动生成数据库?
【发布时间】:2014-06-10 16:29:43
【问题描述】:

我的 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。 但它并没有这样做。

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

有可能吗?

【问题讨论】:

  • 添加“数据库”是指架构吗?
  • 是的,我的意思是单词数据库的架构
  • 我不知道这是否可能。因为 Hibernate 还必须创建数据库用户,包括密码等。

标签: java hibernate schema


【解决方案1】:

看看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>

【讨论】:

  • 是的,生成表格和相关元数据; OP 正在询问将在其中创建这些表的数据库。
  • 好的,但这里的问题是,这个人希望创建多少次架构?我永远不必担心让 Hibernate 创建模式,因为我每台机器只做一次。
  • 仍然没有回答他的问题吗?
  • "但是我想自动创建数据库和表。可以吗?"我告诉他如何创建表格。
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2016-03-08
  • 2016-08-16
  • 2010-11-19
  • 2014-05-15
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多