简体   繁体   中英

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

Schema is not created automatically if not exist how to solve,if database name exist means tables are create automatically but schema is not exists means not created schema at run time how to do.

hibernate properties

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>

There is no such value as create-update for the property hibernate.hbm2ddl.auto . All possible values are

  • validate
  • update
  • create
  • create-drop

Please read the documentation . You can use create to create a schema, if not exists

You can use import.sql .

Add a import.sql file in resource as follow:

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

and add a line in hibernate.cfg.xml as follow:

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

So, if not exist the database, hibernate creates new db.

your hibernate.hbm2ddl.auto setting should be defining that the database is created ( options are validate, create, update or create-drop )

There is also the undocumented value of " none " to disable it entirely.

defined as below

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

A value of create will create your tables at sessionFactory creation, and leave them intact.

A value of create-drop will create your tables, and then drop them when you close the sessionFactory .

validate : validate the schema, makes no changes to the database.

update : update the schema.

One can use select value as well for hibernate.hbm2ddl.auto instead of create. We are using as below in our application and it is working fantastic. I think this is the first information on StackOverflow on select value for hibernate.hbm2ddl.auto as I googled but in vain could not find any links on select value for "hibernate.hbm2ddl.auto" property.

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

In case anyone has further inputs on select option. Please share.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM