簡體   English   中英

如果不存在Hibernate4和spring4 for mysql,則不會自動創建架構

[英]Schema is not created automatically if not exist Hibernate4 and spring4 for mysql

如果不存在則不自動創建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>  

在代碼中使用它,它會刪除當前架構並創建一個新架構。

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

對於屬性hibernate.hbm2ddl.auto沒有create-update這樣的值。 所有可能的值都是

  • 驗證
  • 更新
  • 創建
  • 創建降

請閱讀文檔 您可以使用create來創建模式(如果不存在)

您可以使用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 :驗證模式,不對數據庫進行任何更改。

更新 :更新架構。

也可以為hibernate.hbm2ddl.auto而不是create使用select值。 我們在我們的應用程序中使用如下,它工作得非常好。 我認為這是關於hibernate.hbm2ddl.auto選擇值的StackOverflow上的第一個信息,因為我用Google搜索但是徒勞無法找到“hibernate.hbm2ddl.auto”屬性的select值的任何鏈接。

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

如果任何人對選擇選項有進一步的輸入。 請分享。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM