【问题标题】:Schema is not created automatically if not exist Hibernate4 and spring4 for mysql如果 mysql 不存在 Hibernate 4 和 spring 4,则不会自动创建模式
【发布时间】:2014-11-02 10:13:20
【问题描述】:

Schema不存在不自动创建如何解决,如果数据库名存在表示自动创建表但schema不存在表示在运行时没有创建schema怎么办。

休眠属性

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-update ** i use these keyword seperately also //create or//update**



**xml configuration**

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.testing.domain" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                </prop>
            </props>
        </property>
    </bean>  

【问题讨论】:

    标签: java mysql spring hibernate


    【解决方案1】:

    在您的代码中使用它,它会删除当前架构并创建一个新架构。

               <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto.create-drop}</prop>
    

    【讨论】:

    • 然后使用 ${hibernate.hbm2ddl.auto.create}
    • 这些仅在运行时顺序创建新架构而不是更新无法检索旧记录,如果不存在,我需要一次配置来创建架构,并且还要不断更新记录
    【解决方案2】:

    您可以使用import.sql

    在资源中添加import.sql文件如下:

    /*create database at first time*/ 
    CREATE SCHEMA your-database-name;
    

    并在hibernate.cfg.xml 中添加一行如下:

    <hibernate-configuration>
        <session-factory>
           ...
           ...
           <property name="hbm2ddl.import_files">import.sql</property>
           ...
           ...
        </session-factory>
    </hibernate-configuration>
    

    所以,如果数据库不存在,hibernate 会创建新的数据库。

    您的 hibernate.hbm2ddl.auto 设置应定义创建数据库 (options are validate, create, update or create-drop)

    还有“none”的未记录值可以完全禁用它。

    定义如下

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

    create 的值将在 sessionFactory 创建时创建您的表,并保持它们不变。

    create-drop 值将创建您的表,然后在您关闭 sessionFactory 时删除它们。

    validate:验证架构,不更改数据库。

    更新:更新架构。

    【讨论】:

    • 创建模式您的数据库名称;我必须在哪里配置
    • @user3928299 在import.sql里面,这只是创建schema,如果不存在,hibernate不会创建自己的scheme实例,它只是创建/更新表/列
    • 我找到了解决方案 database.url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8;大
    【解决方案3】:

    属性hibernate.hbm2ddl.auto 没有像create-update 这样的值。所有可能的值都是

    • 验证
    • 更新
    • 创建
    • 创建删除

    请阅读documentation。如果不存在,您可以使用create 创建架构

    【讨论】:

      【解决方案4】:

      也可以对 hibernate.hbm2ddl.auto 使用选择值而不是创建。我们在我们的应用程序中使用如下,它工作得很好。我认为这是 StackOverflow 上关于 hibernate.hbm2ddl.auto 的选择值的第一个信息,因为我在 Google 上搜索过,但没有找到关于“hibernate.hbm2ddl.auto”属性的选择值的任何链接。

      <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <beans:property name="dataSource" ref="databaseDataSource" />
          <beans:property name="hibernateProperties">
              <beans:props>
                  <beans:prop key="hibernate.hbm2ddl.auto">select</beans:prop>
              </beans:props>
          </beans:property>
      </beans:bean>
      

      如果有人对选择选项有进一步的输入。请分享。

      【讨论】:

      • 看起来 hibernate.hbm2ddl.auto 的选择选项仅用于查询数据库。这意味着应用程序不再更改 DDL。如有错误请指正。
      • database.url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8;
      猜你喜欢
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2021-09-15
      • 2019-06-17
      相关资源
      最近更新 更多