4

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>  
Basker Ammu
  • 105
  • 1
  • 7

4 Answers4

2

use this in your code, it drop the current schema and create a new one.

           <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto.create-drop}</prop>
Killer
  • 592
  • 1
  • 6
  • 24
  • then use ${hibernate.hbm2ddl.auto.create} – Killer Sep 09 '14 at 05:51
  • these only create sequencly at run time new schema not for update not possible to retrieve for old record,i need one time config to create schema if not exist and also continously to update record – Basker Ammu Sep 09 '14 at 06:18
  • check this [link] http://stackoverflow.com/questions/306806/hibernate-automatically-creating-updating-the-db-tables-based-on-entity-classes – Killer Sep 09 '14 at 06:27
0

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

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

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.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • CREATE SCHEMA your-database-name; where i have to configure – Basker Ammu Sep 09 '14 at 06:12
  • @user3928299 inside `import.sql`, this is just to create the schema if not exists, hibernate does not create scheme instance of its own, it just creates/updates tables/columns – Ankur Singhal Sep 09 '14 at 06:14
  • i found solution database.url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8; da – Basker Ammu Sep 10 '14 at 06:10
0

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.

recnac
  • 3,744
  • 6
  • 24
  • 46
AK Gangoni
  • 124
  • 6
  • Looks like select option for hibernate.hbm2ddl.auto is used to just query DB. This means no more DDL changes from application. Please correct if wrong. – AK Gangoni Sep 09 '14 at 09:45
  • database.url=jdbc:mysql://localhost:3306/hicasting?createDatabaseIfNotExist=true&characterEncoding=UTF-8; – Basker Ammu Sep 10 '14 at 06:09